Skip to content

Commit 000c9b4

Browse files
committed
fix: Allow replacing default ssh config file in tests
1 parent 5ed5e0e commit 000c9b4

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

cli/configssh.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,9 @@ var (
5757
// sshCoderConfigOptions represents options that can be stored and read
5858
// from the coder config in ~/.ssh/coder.
5959
type sshCoderConfigOptions struct {
60-
sshConfigFile string
61-
sshOptions []string
62-
}
63-
64-
func (o sshCoderConfigOptions) isZero() bool {
65-
return o.sshConfigFile == sshDefaultConfigFileName && len(o.sshOptions) == 0
60+
sshConfigDefaultFile string
61+
sshConfigFile string
62+
sshOptions []string
6663
}
6764

6865
func (o sshCoderConfigOptions) equal(other sshCoderConfigOptions) bool {
@@ -75,7 +72,7 @@ func (o sshCoderConfigOptions) equal(other sshCoderConfigOptions) bool {
7572
}
7673

7774
func (o sshCoderConfigOptions) asArgs() (args []string) {
78-
if o.sshConfigFile != sshDefaultConfigFileName {
75+
if o.sshConfigFile != o.sshConfigDefaultFile {
7976
args = append(args, "--ssh-config-file", o.sshConfigFile)
8077
}
8178
for _, opt := range o.sshOptions {
@@ -85,7 +82,7 @@ func (o sshCoderConfigOptions) asArgs() (args []string) {
8582
}
8683

8784
func (o sshCoderConfigOptions) asList() (list []string) {
88-
if o.sshConfigFile != sshDefaultConfigFileName {
85+
if o.sshConfigFile != o.sshConfigDefaultFile {
8986
list = append(list, fmt.Sprintf("ssh-config-file: %s", o.sshConfigFile))
9087
}
9188
for _, opt := range o.sshOptions {
@@ -175,7 +172,7 @@ func configSSH() *cobra.Command {
175172
return xerrors.Errorf("unexpected content in %s: remove the file and rerun the command to continue", coderConfigFile)
176173
}
177174
}
178-
lastCoderConfig := sshCoderConfigParseLastOptions(bytes.NewReader(coderConfigRaw))
175+
lastCoderConfig := sshCoderConfigParseLastOptions(bytes.NewReader(coderConfigRaw), coderConfig.sshConfigDefaultFile)
179176

180177
// Only prompt when no arguments are provided and avoid
181178
// prompting in diff mode (unexpected behavior).
@@ -368,8 +365,10 @@ func configSSH() *cobra.Command {
368365
},
369366
}
370367
cliflag.StringVarP(cmd.Flags(), &coderConfig.sshConfigFile, "ssh-config-file", "", "CODER_SSH_CONFIG_FILE", sshDefaultConfigFileName, "Specifies the path to an SSH config.")
371-
cmd.Flags().StringVar(&coderConfigFile, "ssh-coder-config-file", sshDefaultCoderConfigFileName, "Specifies the path to an Coder SSH config file. Useful for testing.")
372-
_ = cmd.Flags().MarkHidden("ssh-coder-config-file")
368+
cmd.Flags().StringVar(&coderConfig.sshConfigDefaultFile, "test.default-ssh-config-file", sshDefaultConfigFileName, "Specifies the default path to the SSH config file. Useful for testing.")
369+
_ = cmd.Flags().MarkHidden("test.default-ssh-config-file")
370+
cmd.Flags().StringVar(&coderConfigFile, "test.ssh-coder-config-file", sshDefaultCoderConfigFileName, "Specifies the path to an Coder SSH config file. Useful for testing.")
371+
_ = cmd.Flags().MarkHidden("test.ssh-coder-config-file")
373372
cmd.Flags().StringArrayVarP(&coderConfig.sshOptions, "ssh-option", "o", []string{}, "Specifies additional SSH options to embed in each host stanza.")
374373
cmd.Flags().BoolVarP(&showDiff, "diff", "D", false, "Show diff of changes that will be made.")
375374
cmd.Flags().BoolVarP(&skipProxyCommand, "skip-proxy-command", "", false, "Specifies whether the ProxyCommand option should be skipped. Useful for testing.")
@@ -418,7 +417,7 @@ func sshCoderConfigWriteHeader(w io.Writer, o sshCoderConfigOptions) error {
418417
_, _ = fmt.Fprint(w, sshCoderConfigHeader)
419418
_, _ = fmt.Fprint(w, sshCoderConfigDocsHeader)
420419
_, _ = fmt.Fprint(w, sshCoderConfigOptionsHeader)
421-
if o.sshConfigFile != sshDefaultConfigFileName {
420+
if o.sshConfigFile != o.sshConfigDefaultFile {
422421
_, _ = fmt.Fprintf(w, "# :%s=%s\n", "ssh-config-file", o.sshConfigFile)
423422
}
424423
for _, opt := range o.sshOptions {
@@ -428,8 +427,9 @@ func sshCoderConfigWriteHeader(w io.Writer, o sshCoderConfigOptions) error {
428427
return nil
429428
}
430429

431-
func sshCoderConfigParseLastOptions(r io.Reader) (o sshCoderConfigOptions) {
432-
o.sshConfigFile = sshDefaultConfigFileName // Default value is not written.
430+
func sshCoderConfigParseLastOptions(r io.Reader, sshConfigDefaultFile string) (o sshCoderConfigOptions) {
431+
o.sshConfigDefaultFile = sshConfigDefaultFile
432+
o.sshConfigFile = sshConfigDefaultFile // Default value is not written.
433433

434434
s := bufio.NewScanner(r)
435435
for s.Scan() {

0 commit comments

Comments
 (0)