Skip to content

Commit de8df06

Browse files
committed
feat: add --ssh-host-prefix flag to "coder ssh"
This adds a flag matching `--ssh-host-prefix` from `coder config-ssh` to `coder ssh`. By trimming a custom prefix from the argument, we can set up wildcard-based `Host` entries in SSH config for the IDE plugins (and eventually `coder config-ssh`). We also replace `--` in the argument with `/`, so ownership can be specified in wildcard-based SSH hosts like `<owner>--<workspace>`.
1 parent 8a7b5dc commit de8df06

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

cli/ssh.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ var (
5656
func (r *RootCmd) ssh() *serpent.Command {
5757
var (
5858
stdio bool
59+
hostPrefix string
5960
forwardAgent bool
6061
forwardGPG bool
6162
identityAgent string
@@ -180,15 +181,9 @@ func (r *RootCmd) ssh() *serpent.Command {
180181
parsedEnv = append(parsedEnv, [2]string{k, v})
181182
}
182183

183-
namedWorkspace := inv.Args[0]
184-
if parts := strings.Split(namedWorkspace, "--"); len(parts) >= 3 {
185-
owner := parts[1]
186-
name := parts[2]
187-
namedWorkspace = owner + "/" + name
188-
if len(parts) > 3 {
189-
namedWorkspace += "." + parts[3]
190-
}
191-
}
184+
namedWorkspace := strings.TrimPrefix(inv.Args[0], hostPrefix)
185+
// Support "--" as a delimiter between owner and workspace name
186+
namedWorkspace = strings.ReplaceAll(namedWorkspace, "--", "/")
192187

193188
workspace, workspaceAgent, err := getWorkspaceAndAgent(ctx, inv, client, !disableAutostart, namedWorkspace)
194189
if err != nil {
@@ -487,6 +482,12 @@ func (r *RootCmd) ssh() *serpent.Command {
487482
Description: "Specifies whether to emit SSH output over stdin/stdout.",
488483
Value: serpent.BoolOf(&stdio),
489484
},
485+
{
486+
Flag: "ssh-host-prefix",
487+
Env: "CODER_CONFIGSSH_SSH_HOST_PREFIX",
488+
Description: "Strip this prefix from the provided hostname to determine the workspace name.",
489+
Value: serpent.StringOf(&hostPrefix),
490+
},
490491
{
491492
Flag: "forward-agent",
492493
FlagShorthand: "A",

cli/ssh_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ func TestSSH(t *testing.T) {
14831483
}
14841484
})
14851485

1486-
t.Run("ParseHostname", func(t *testing.T) {
1486+
t.Run("SSHHostPrefix", func(t *testing.T) {
14871487
t.Parallel()
14881488
client, workspace, agentToken := setupWorkspaceForAgent(t)
14891489
_, _ = tGoContext(t, func(ctx context.Context) {
@@ -1507,7 +1507,7 @@ func TestSSH(t *testing.T) {
15071507
user, err := client.User(ctx, codersdk.Me)
15081508
require.NoError(t, err)
15091509

1510-
inv, root := clitest.New(t, "ssh", "--stdio", fmt.Sprintf("coder-vscode.coder.prod.netflix.net--%s--%s", user.Username, workspace.Name))
1510+
inv, root := clitest.New(t, "ssh", "--stdio", "--ssh-host-prefix", "coder.dummy.com--", fmt.Sprintf("coder.dummy.com--%s--%s", user.Username, workspace.Name))
15111511
clitest.SetupConfig(t, client, root)
15121512
inv.Stdin = clientOutput
15131513
inv.Stdout = serverInput

cli/testdata/coder_ssh_--help.golden

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ OPTIONS:
3939
-R, --remote-forward string-array, $CODER_SSH_REMOTE_FORWARD
4040
Enable remote port forwarding (remote_port:local_address:local_port).
4141

42+
--ssh-host-prefix string, $CODER_CONFIGSSH_SSH_HOST_PREFIX
43+
Strip this prefix from the provided hostname to determine the
44+
workspace name.
45+
4246
--stdio bool, $CODER_SSH_STDIO
4347
Specifies whether to emit SSH output over stdin/stdout.
4448

docs/reference/cli/ssh.md

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)