Skip to content

feat: Refactor CLI config-ssh to improve UX #1900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 38 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
b7f2b3d
feat: Refactor CLI config-ssh to improve UX
mafredri May 30, 2022
4f03415
chore: Switch to github.com/pkg/diff for diffing
mafredri May 30, 2022
e196920
fix: Output absolute paths for diffs
mafredri May 30, 2022
cdcf6c3
chore: Remove todone
mafredri May 30, 2022
263d823
chore: Rename actions => changes for consistency
mafredri May 30, 2022
18797e7
fix: Remove workspaces check, removing is a legit use-case
mafredri May 30, 2022
9680792
fix: Remove unused else
mafredri May 30, 2022
7a4b8d6
fix: Handle no workspaces case
mafredri May 30, 2022
e552e84
chore: Update diffBytes comment
mafredri May 30, 2022
5250a8a
fix: Cleanup temp file on failure
mafredri May 30, 2022
0749e58
fix: Wrap error
mafredri May 30, 2022
b3f448a
fix: Enable color when terminal is a tty
mafredri May 30, 2022
c5f1983
fix: Improve output, detect if stdout is tty
mafredri May 31, 2022
8d0f72c
fix: Add Include to the top of config, verify semantically
mafredri May 31, 2022
d7838b5
fix: Guard against overwriting unknown ~/.ssh/coder file
mafredri May 31, 2022
f94bc3f
feat: Parse and prompt to re-use previous configuration
mafredri May 31, 2022
00a8c12
chore: Increase indentation
mafredri May 31, 2022
e50c4c3
chore: Remove unused equality func
mafredri May 31, 2022
94c8681
fix: Perform multi line regexp matches
mafredri Jun 1, 2022
1e33886
fix: Only rewrite coder file path if not ~/
mafredri Jun 1, 2022
209c8ff
fix: Allow removing invalid Include coder entry
mafredri Jun 1, 2022
5ed5e0e
Revert "chore: Remove unused equality func"
mafredri Jun 1, 2022
000c9b4
fix: Allow replacing default ssh config file in tests
mafredri Jun 1, 2022
9d64680
feat: Update options prompt, default to using new options
mafredri Jun 1, 2022
a7cb149
fix: Improve regexes
mafredri Jun 1, 2022
a77c791
feat: Add tests
mafredri Jun 1, 2022
5bcd038
fix: Do not prompt for previous opts on first run
mafredri Jun 1, 2022
1632eb4
fix: Fix broken TestConfigSSH test
mafredri Jun 1, 2022
99693ce
fix: Revert multiwriter change usued while testing
mafredri Jun 1, 2022
fb24742
fix: Change diff suggestions for more natural sentence
mafredri Jun 1, 2022
5c785dd
fix: Typo
mafredri Jun 1, 2022
c60762f
fix: Fix linting (gocyclo, revive), improve responsiveness
mafredri Jun 1, 2022
0422e71
chore: Update header comment
mafredri Jun 6, 2022
6e1e1a0
chore: Extract function
mafredri Jun 6, 2022
fda935f
chore: Remove test log statements
mafredri Jun 6, 2022
f1be4c6
Make Include regexp stricter to avoid deleting user data
mafredri Jun 8, 2022
bf4596c
chore: Update the "Changes" text for diff to match non-diff mode
mafredri Jun 8, 2022
093e2ce
chore: Use consistent naming of functions
mafredri Jun 8, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: Switch to github.com/pkg/diff for diffing
  • Loading branch information
mafredri committed Jun 8, 2022
commit 4f03415e073e709697794e29bcdff27d91da1212
50 changes: 19 additions & 31 deletions cli/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"

"github.com/cli/safeexec"
"github.com/pkg/diff"
"github.com/pkg/diff/write"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -383,40 +384,27 @@ func currentBinPath(cmd *cobra.Command) (string, error) {

// diffBytes two byte slices as if they were in a file named name.
// Does best-effort cleanup ignoring non-critical errors.
func diffBytes(name string, b1, b2 []byte) (data []byte, err error) {
f1, err := os.CreateTemp("", "coder_config-ssh.")
if err != nil {
return nil, xerrors.Errorf("create temp 1 file failed: %w", err)
}
defer os.Remove(f1.Name())
defer f1.Close()

f2, err := os.CreateTemp("", "coder_config-ssh.")
if err != nil {
return nil, xerrors.Errorf("create temp 2 file failed: %w", err)
func diffBytes(name string, b1, b2 []byte) ([]byte, error) {
var buf bytes.Buffer
var opts []write.Option
// TODO(mafredri): Toggle color on/off
if false {
opts = append(opts, write.TerminalColor())
}
defer os.Remove(f2.Name())
defer f2.Close()

_, err = f1.Write(b1)
err := diff.Text(name, name+".new", b1, b2, &buf, opts...)
if err != nil {
return nil, xerrors.Errorf("write temp 1 file failed: %w", err)
}
_, err = f2.Write(b2)
if err != nil {
return nil, xerrors.Errorf("write temp 2 file failed: %w", err)
}

// TODO(mafredri): Ensure diff binary exists, or return useful error when missing.
data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).Output() // #nosec
if len(data) == 0 && err != nil { // Ignore non-zero exit when files differ.
return nil, err
}
// Replace temp file names with friendly names.
data = bytes.Replace(data, []byte(f1.Name()), []byte(name), 1)
data = bytes.Replace(data, []byte(f2.Name()), []byte(name+".new"), 1)

return data, err
b := buf.Bytes()
// Check if diff only output two lines, if yes, there's no diff.
//
// Example:
// --- ~/.ssh/config
// +++ ~/.ssh/config.new
if bytes.Count(b, []byte{'\n'}) == 2 {
b = nil
}
return b, nil
}

// stripOldConfigBlock is here to migrate users from old config block
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ require (
github.com/pion/udp v0.1.1
github.com/pion/webrtc/v3 v3.1.41
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
github.com/pkg/sftp v1.13.4
github.com/prometheus/client_golang v1.12.2
github.com/quasilyte/go-ruleguard/dsl v0.3.21
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,7 @@ github.com/pires/go-proxyproto v0.6.2/go.mod h1:Odh9VFOZJCf9G8cLW5o435Xf1J95Jw9G
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down