Skip to content

Commit 9b44ab0

Browse files
committed
fix: Allow replacing default ssh config file in tests
1 parent 83039c2 commit 9b44ab0

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 {
@@ -177,7 +174,7 @@ func configSSH() *cobra.Command {
177174
return xerrors.Errorf("unexpected content in %s: remove the file and rerun the command to continue", coderConfigFile)
178175
}
179176
}
180-
lastCoderConfig := sshCoderConfigParseLastOptions(bytes.NewReader(coderConfigRaw))
177+
lastCoderConfig := sshCoderConfigParseLastOptions(bytes.NewReader(coderConfigRaw), coderConfig.sshConfigDefaultFile)
181178

182179
// Only prompt when no arguments are provided and avoid
183180
// prompting in diff mode (unexpected behavior).
@@ -370,8 +367,10 @@ func configSSH() *cobra.Command {
370367
},
371368
}
372369
cliflag.StringVarP(cmd.Flags(), &coderConfig.sshConfigFile, "ssh-config-file", "", "CODER_SSH_CONFIG_FILE", sshDefaultConfigFileName, "Specifies the path to an SSH config.")
373-
cmd.Flags().StringVar(&coderConfigFile, "ssh-coder-config-file", sshDefaultCoderConfigFileName, "Specifies the path to an Coder SSH config file. Useful for testing.")
374-
_ = cmd.Flags().MarkHidden("ssh-coder-config-file")
370+
cmd.Flags().StringVar(&coderConfig.sshConfigDefaultFile, "test.default-ssh-config-file", sshDefaultConfigFileName, "Specifies the default path to the SSH config file. Useful for testing.")
371+
_ = cmd.Flags().MarkHidden("test.default-ssh-config-file")
372+
cmd.Flags().StringVar(&coderConfigFile, "test.ssh-coder-config-file", sshDefaultCoderConfigFileName, "Specifies the path to an Coder SSH config file. Useful for testing.")
373+
_ = cmd.Flags().MarkHidden("test.ssh-coder-config-file")
375374
cmd.Flags().StringArrayVarP(&coderConfig.sshOptions, "ssh-option", "o", []string{}, "Specifies additional SSH options to embed in each host stanza.")
376375
cmd.Flags().BoolVarP(&showDiff, "diff", "D", false, "Show diff of changes that will be made.")
377376
cmd.Flags().BoolVarP(&skipProxyCommand, "skip-proxy-command", "", false, "Specifies whether the ProxyCommand option should be skipped. Useful for testing.")
@@ -420,7 +419,7 @@ func sshCoderConfigWriteHeader(w io.Writer, o sshCoderConfigOptions) error {
420419
_, _ = fmt.Fprint(w, sshCoderConfigHeader)
421420
_, _ = fmt.Fprint(w, sshCoderConfigDocsHeader)
422421
_, _ = fmt.Fprint(w, sshCoderConfigOptionsHeader)
423-
if o.sshConfigFile != sshDefaultConfigFileName {
422+
if o.sshConfigFile != o.sshConfigDefaultFile {
424423
_, _ = fmt.Fprintf(w, "# :%s=%s\n", "ssh-config-file", o.sshConfigFile)
425424
}
426425
for _, opt := range o.sshOptions {
@@ -430,8 +429,9 @@ func sshCoderConfigWriteHeader(w io.Writer, o sshCoderConfigOptions) error {
430429
return nil
431430
}
432431

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

436436
s := bufio.NewScanner(r)
437437
for s.Scan() {

0 commit comments

Comments
 (0)