8000 fix: Clean up `coder config-ssh` dry-run behavior by mafredri · Pull Request #3660 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix: Clean up coder config-ssh dry-run behavior #3660

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 4 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
95 changes: 32 additions & 63 deletions cli/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ func configSSH() *cobra.Command {
sshConfigFile string
sshConfigOpts sshConfigOptions
usePreviousOpts bool
coderConfigFile string
dryRun bool
skipProxyCommand bool
wireguard bool
Expand Down Expand Up @@ -198,15 +197,7 @@ func configSSH() *cobra.Command {
// Parse the previous configuration only if config-ssh
// has been run previously.
var lastConfig *sshConfigOptions
var ok bool
var coderConfigRaw []byte
if coderConfigFile, coderConfigRaw, ok = readDeprecatedCoderConfigFile(homedir, coderConfigFile); ok {
// Deprecated: Remove after migration period.
changes = append(changes, fmt.Sprintf("Remove old auto-generated coder config file at %s", coderConfigFile))
// Backwards compate, restore old options.
c := sshConfigParseLastOptions(bytes.NewReader(coderConfigRaw))
lastConfig = &c
} else if section, ok := sshConfigGetCoderSection(configRa 8000 w); ok {
if section, ok := sshConfigGetCoderSection(configRaw); ok {
c := sshConfigParseLastOptions(bytes.NewReader(section))
lastConfig = &c
}
Expand Down Expand Up @@ -237,6 +228,8 @@ func configSSH() *cobra.Command {
}
// Selecting "no" will use the last config.
sshConfigOpts = *lastConfig
} else {
changes = append(changes, "Use new SSH options")
}
// Only print when prompts are shown.
if yes, _ := cmd.Flags().GetBool("yes"); !yes {
Expand All @@ -245,14 +238,6 @@ func configSSH() *cobra.Command {
}

configModified := configRaw

// Check for the presence of the coder Include
// statement is present and add if missing.
// Deprecated: Remove after migration period.
if configModified, ok = removeDeprecatedSSHIncludeStatement(configModified); ok {
changes = append(changes, fmt.Sprintf("Remove %q from %s", "Include coder", sshConfigFile))
}

root := createConfig(cmd)

buf := &bytes.Buffer{}
Expand Down Expand Up @@ -313,17 +298,34 @@ func configSSH() *cobra.Command {
_, _ = buf.Write(after)

if !bytes.Equal(configModified, buf.Bytes()) {
changes = append(changes, fmt.Sprintf("Update coder config section in %s", sshConfigFile))
changes = append(changes, fmt.Sprintf("Update the coder section in %s", sshConfigFile))
configModified = buf.Bytes()
}

if len(changes) > 0 {
dryRunDisclaimer := ""
if dryRun {
dryRunDisclaimer = " (dry-run, no changes will be made)"
if len(changes) == 0 {
_, _ = fmt.Fprintf(out, "No changes to make.\n")
return nil
}

if dryRun {
_, _ = fmt.Fprintf(out, "Dry run, the following changes would be made to your SSH configuration:\n\n * %s\n\n", strings.Join(changes, "\n * "))

color := isTTYOut(cmd)
diff, err := diffBytes(sshConfigFile, configRaw, configModified, color)
if err != nil {
return xerrors.Errorf("diff failed: %w", err)
}
if len(diff) > 0 {
// Write diff to stdout.
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s", diff)
}

return nil
}

if len(changes) > 0 {
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
Text: fmt.Sprintf("The following changes will be made to your SSH configuration:\n\n * %s\n\n Continue?%s", strings.Join(changes, "\n * "), dryRunDisclaimer),
Text: fmt.Sprintf("The following changes will be made to your SSH configuration:\n\n * %s\n\n Continue?", strings.Join(changes, "\n * ")),
IsConfirm: true,
})
if err != nil {
Expand All @@ -335,47 +337,18 @@ func configSSH() *cobra.Command {
}
}

if dryRun {
color := isTTYOut(cmd)
diffFns := []func() ([]byte, error){
func() ([]byte, error) { return diffBytes(sshConfigFile, configRaw, configModified, color) },
}
if len(coderConfigRaw) > 0 {
// Deprecated: Remove after migration period.
diffFns = append(diffFns, func() ([]byte, error) { return diffBytes(coderConfigFile, coderConfigRaw, nil, color) })
}

for _, diffFn := range diffFns {
diff, err := diffFn()
if err != nil {
return xerrors.Errorf("diff failed: %w", err)
}
if len(diff) > 0 {
// Write diff to stdout.
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "\n%s", diff)
}
}
} else {
if !bytes.Equal(configRaw, configModified) {
err = writeWithTempFileAndMove(sshConfigFile, bytes.NewReader(configModified))
if err != nil {
return xerrors.Errorf("write ssh config failed: %w", err)
}
}
// Deprecated: Remove after migration period.
if len(coderConfigRaw) > 0 {
err = os.Remove(coderConfigFile)
if err != nil {
return xerrors.Errorf("remove coder config failed: %w", err)
}
if !bytes.Equal(configRaw, configModified) {
err = writeWithTempFileAndMove(sshConfigFile, bytes.NewReader(configModified))
if err != nil {
return xerrors.Errorf("write ssh config failed: %w", err)
}
}

if len(workspaceConfigs) > 0 {
_, _ = fmt.Fprintln(out, "You should now be able to ssh into your workspace.")
_, _ = fmt.Fprintf(out, "For example, try running:\n\n\t$ ssh coder.%s\n\n", workspaceConfigs[0].Name)
_, _ = fmt.Fprintf(out, "For example, try running:\n\n\t$ ssh coder.%s\n", workspaceConfigs[0].Name)
} else {
_, _ = fmt.Fprint(out, "You don't have any workspaces yet, try creating one with:\n\n\t$ coder create <workspace>\n\n")
_, _ = fmt.Fprint(out, "You don't have any workspaces yet, try creating one with:\n\n\t$ coder create <workspace>\n")
}
return nil
},
Expand All @@ -389,10 +362,6 @@ func configSSH() *cobra.Command {
cliflag.BoolVarP(cmd.Flags(), &wireguard, "wireguard", "", "CODER_CONFIG_SSH_WIREGUARD", false, "Whether to use Wireguard for SSH tunneling.")
_ = cmd.Flags().MarkHidden("wireguard")

// Deprecated: Remove after migration period.
cmd.Flags().StringVar(&coderConfigFile, "test.ssh-coder-config-file", sshDefaultCoderConfigFileName, "Specifies the path to an Coder SSH config file. Useful for testing.")
_ = cmd.Flags().MarkHidden("test.ssh-coder-config-file")

cliui.AllowSkipPrompt(cmd)

return cmd
Expand Down
66 changes: 0 additions & 66 deletions cli/configssh_old.go

This file was deleted.

Loading
0