From 877205adb44ab62b4b886ab1c71f2c8ec521afe0 Mon Sep 17 00:00:00 2001 From: Spike Curtis Date: Mon, 7 Apr 2025 06:27:10 +0000 Subject: [PATCH 1/2] feat: add hostname-suffix option to config-ssh --- cli/configssh.go | 28 +++++++++++++++++++-- cli/configssh_test.go | 2 ++ cli/testdata/coder_config-ssh_--help.golden | 3 +++ docs/reference/cli/config-ssh.md | 9 +++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/cli/configssh.go b/cli/configssh.go index 952120c30b477..0fc496927b19f 100644 --- a/cli/configssh.go +++ b/cli/configssh.go @@ -45,8 +45,10 @@ const ( // sshConfigOptions represents options that can be stored and read // from the coder config in ~/.ssh/coder. type sshConfigOptions struct { - waitEnum string + waitEnum string + // Deprecated: moving away from prefix to hostnameSuffix userHostPrefix string + hostnameSuffix string sshOptions []string disableAutostart bool header []string @@ -97,7 +99,11 @@ func (o sshConfigOptions) equal(other sshConfigOptions) bool { if !slicesSortedEqual(o.header, other.header) { return false } - return o.waitEnum == other.waitEnum && o.userHostPrefix == other.userHostPrefix && o.disableAutostart == other.disableAutostart && o.headerCommand == other.headerCommand + return o.waitEnum == other.waitEnum && + o.userHostPrefix == other.userHostPrefix && + o.disableAutostart == other.disableAutostart && + o.headerCommand == other.headerCommand && + o.hostnameSuffix == other.hostnameSuffix } // slicesSortedEqual compares two slices without side-effects or regard to order. @@ -119,6 +125,9 @@ func (o sshConfigOptions) asList() (list []string) { if o.userHostPrefix != "" { list = append(list, fmt.Sprintf("ssh-host-prefix: %s", o.userHostPrefix)) } + if o.hostnameSuffix != "" { + list = append(list, fmt.Sprintf("hostname-suffix: %s", o.hostnameSuffix)) + } if o.disableAutostart { list = append(list, fmt.Sprintf("disable-autostart: %v", o.disableAutostart)) } @@ -314,6 +323,10 @@ func (r *RootCmd) configSSH() *serpent.Command { // Override with user flag. coderdConfig.HostnamePrefix = sshConfigOpts.userHostPrefix } + if sshConfigOpts.hostnameSuffix != "" { + // Override with user flag. + coderdConfig.HostnameSuffix = sshConfigOpts.hostnameSuffix + } // Write agent configuration. defaultOptions := []string{ @@ -518,6 +531,12 @@ func (r *RootCmd) configSSH() *serpent.Command { Description: "Override the default host prefix.", Value: serpent.StringOf(&sshConfigOpts.userHostPrefix), }, + { + Flag: "hostname-suffix", + Env: "CODER_CONFIGSSH_HOST_SUFFIX", + Description: "Override the default host suffix.", + Value: serpent.StringOf(&sshConfigOpts.hostnameSuffix), + }, { Flag: "wait", Env: "CODER_CONFIGSSH_WAIT", // Not to be mixed with CODER_SSH_WAIT. @@ -568,6 +587,9 @@ func sshConfigWriteSectionHeader(w io.Writer, addNewline bool, o sshConfigOption if o.userHostPrefix != "" { _, _ = fmt.Fprintf(&ow, "# :%s=%s\n", "ssh-host-prefix", o.userHostPrefix) } + if o.hostnameSuffix != "" { + _, _ = fmt.Fprintf(&ow, "# :%s=%s\n", "hostname-suffix", o.hostnameSuffix) + } if o.disableAutostart { _, _ = fmt.Fprintf(&ow, "# :%s=%v\n", "disable-autostart", o.disableAutostart) } @@ -607,6 +629,8 @@ func sshConfigParseLastOptions(r io.Reader) (o sshConfigOptions) { o.waitEnum = parts[1] case "ssh-host-prefix": o.userHostPrefix = parts[1] + case "hostname-suffix": + o.hostnameSuffix = parts[1] case "ssh-option": o.sshOptions = append(o.sshOptions, parts[1]) case "disable-autostart": diff --git a/cli/configssh_test.go b/cli/configssh_test.go index 3b88ab1e54db7..84399ddc67949 100644 --- a/cli/configssh_test.go +++ b/cli/configssh_test.go @@ -432,6 +432,7 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) { "# Last config-ssh options:", "# :wait=yes", "# :ssh-host-prefix=coder-test.", + "# :hostname-suffix=coder-suffix", "# :header=X-Test-Header=foo", "# :header=X-Test-Header2=bar", "# :header-command=printf h1=v1 h2=\"v2\" h3='v3'", @@ -447,6 +448,7 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) { "--yes", "--wait=yes", "--ssh-host-prefix", "coder-test.", + "--hostname-suffix", "coder-suffix", "--header", "X-Test-Header=foo", "--header", "X-Test-Header2=bar", "--header-command", "printf h1=v1 h2=\"v2\" h3='v3'", diff --git a/cli/testdata/coder_config-ssh_--help.golden b/cli/testdata/coder_config-ssh_--help.golden index ebbfb7a11676c..cf843dedde9e0 100644 --- a/cli/testdata/coder_config-ssh_--help.golden +++ b/cli/testdata/coder_config-ssh_--help.golden @@ -33,6 +33,9 @@ OPTIONS: unix-like shell. This flag forces the use of unix file paths (the forward slash '/'). + --hostname-suffix string, $CODER_CONFIGSSH_HOST_SUFFIX + Override the default host suffix. + --ssh-config-file string, $CODER_SSH_CONFIG_FILE (default: ~/.ssh/config) Specifies the path to an SSH config. diff --git a/docs/reference/cli/config-ssh.md b/docs/reference/cli/config-ssh.md index 937bcd061bd05..81673eda40ed2 100644 --- a/docs/reference/cli/config-ssh.md +++ b/docs/reference/cli/config-ssh.md @@ -79,6 +79,15 @@ Specifies whether or not to keep options from previous run of config-ssh. Override the default host prefix. +### --hostname-suffix + +| | | +|-------------|-------------------------------------------| +| Type | string | +| Environment | $CODER_CONFIGSSH_HOST_SUFFIX | + +Override the default host suffix. + ### --wait | | | From 632adc9bda0390f0b7b86a751e591f1ed684b435 Mon Sep 17 00:00:00 2001 From: Spike Curtis Date: Mon, 7 Apr 2025 07:41:41 +0000 Subject: [PATCH 2/2] fixup: env and flag match --- cli/configssh.go | 4 ++-- cli/testdata/coder_config-ssh_--help.golden | 4 ++-- docs/reference/cli/config-ssh.md | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cli/configssh.go b/cli/configssh.go index 0fc496927b19f..67fbd19ef3f69 100644 --- a/cli/configssh.go +++ b/cli/configssh.go @@ -533,8 +533,8 @@ func (r *RootCmd) configSSH() *serpent.Command { }, { Flag: "hostname-suffix", - Env: "CODER_CONFIGSSH_HOST_SUFFIX", - Description: "Override the default host suffix.", + Env: "CODER_CONFIGSSH_HOSTNAME_SUFFIX", + Description: "Override the default hostname suffix.", Value: serpent.StringOf(&sshConfigOpts.hostnameSuffix), }, { diff --git a/cli/testdata/coder_config-ssh_--help.golden b/cli/testdata/coder_config-ssh_--help.golden index cf843dedde9e0..86f38db99e84a 100644 --- a/cli/testdata/coder_config-ssh_--help.golden +++ b/cli/testdata/coder_config-ssh_--help.golden @@ -33,8 +33,8 @@ OPTIONS: unix-like shell. This flag forces the use of unix file paths (the forward slash '/'). - --hostname-suffix string, $CODER_CONFIGSSH_HOST_SUFFIX - Override the default host suffix. + --hostname-suffix string, $CODER_CONFIGSSH_HOSTNAME_SUFFIX + Override the default hostname suffix. --ssh-config-file string, $CODER_SSH_CONFIG_FILE (default: ~/.ssh/config) Specifies the path to an SSH config. diff --git a/docs/reference/cli/config-ssh.md b/docs/reference/cli/config-ssh.md index 81673eda40ed2..c9250523b6c28 100644 --- a/docs/reference/cli/config-ssh.md +++ b/docs/reference/cli/config-ssh.md @@ -81,12 +81,12 @@ Override the default host prefix. ### --hostname-suffix -| | | -|-------------|-------------------------------------------| -| Type | string | -| Environment | $CODER_CONFIGSSH_HOST_SUFFIX | +| | | +|-------------|-----------------------------------------------| +| Type | string | +| Environment | $CODER_CONFIGSSH_HOSTNAME_SUFFIX | -Override the default host suffix. +Override the default hostname suffix. ### --wait