Skip to content

Commit 2cac087

Browse files
committed
feat: modify config-ssh to set the host suffix
1 parent 34de3bc commit 2cac087

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

cli/configssh.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,15 @@ func (r *RootCmd) configSSH() *serpent.Command {
356356
if sshConfigOpts.disableAutostart {
357357
flags += " --disable-autostart=true"
358358
}
359+
if coderdConfig.HostnamePrefix != "" {
360+
flags += " --ssh-host-prefix " + coderdConfig.HostnamePrefix
361+
}
362+
if coderdConfig.HostnameSuffix != "" {
363+
flags += " --hostname-suffix " + coderdConfig.HostnameSuffix
364+
}
359365
defaultOptions = append(defaultOptions, fmt.Sprintf(
360-
"ProxyCommand %s %s ssh --stdio%s --ssh-host-prefix %s %%h",
361-
escapedCoderBinary, rootFlags, flags, coderdConfig.HostnamePrefix,
366+
"ProxyCommand %s %s ssh --stdio%s %%h",
367+
escapedCoderBinary, rootFlags, flags,
362368
))
363369
}
364370

@@ -391,7 +397,7 @@ func (r *RootCmd) configSSH() *serpent.Command {
391397
}
392398

393399
hostBlock := []string{
394-
"Host " + coderdConfig.HostnamePrefix + "*",
400+
sshConfigHostLinePatterns(coderdConfig),
395401
}
396402
// Prefix with '\t'
397403
for _, v := range configOptions.sshOptions {
@@ -837,3 +843,19 @@ func diffBytes(name string, b1, b2 []byte, color bool) ([]byte, error) {
837843
}
838844
return b, nil
839845
}
846+
847+
func sshConfigHostLinePatterns(config codersdk.SSHConfigResponse) string {
848+
builder := strings.Builder{}
849+
// by inspection, WriteString always returns nil error
850+
_, _ = builder.WriteString("Host")
851+
if config.HostnamePrefix != "" {
852+
_, _ = builder.WriteString(" ")
853+
_, _ = builder.WriteString(config.HostnamePrefix)
854+
_, _ = builder.WriteString("*")
855+
}
856+
if config.HostnameSuffix != "" {
857+
_, _ = builder.WriteString(" *.")
858+
_, _ = builder.WriteString(config.HostnameSuffix)
859+
}
860+
return builder.String()
861+
}

cli/configssh_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,18 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) {
611611
regexMatch: "RemoteForward 2222 192.168.11.1:2222.*\n.*RemoteForward 2223 192.168.11.1:2223",
612612
},
613613
},
614+
{
615+
name: "Hostname Suffix",
616+
args: []string{
617+
"--yes",
618+
"--hostname-suffix", "testy",
619+
},
620+
wantErr: false,
621+
hasAgent: true,
622+
wantConfig: wantConfig{
623+
regexMatch: `ProxyCommand .* ssh .* --hostname-suffix testy %h`,
624+
},
625+
},
614626
}
615627
for _, tt := range tests {
616628
tt := tt

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy