Skip to content

Commit 025d64b

Browse files
committed
local cli: support additional ssh arguments
1 parent df7929c commit 025d64b

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

components/local-app/cmd/workspace-ssh.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ var dryRun bool
1515
var workspaceSSHCmd = &cobra.Command{
1616
Use: "ssh <workspace-id>",
1717
Short: "Connects to a workspace via SSH",
18-
Args: cobra.ExactArgs(1),
18+
Args: cobra.MinimumNArgs(1),
19+
Example: ` # connect to workspace with current terminal session
20+
$ gitpod workspace ssh <workspace-id>
21+
22+
# Execute simple command in the workspace
23+
$ gitpod workspace ssh <workspace-id> -- ls -la
24+
25+
# Get all SSH features with --dry-run
26+
$ $(gitpod workspace ssh <workspace-id> --dry-run) -- ls -la
27+
$ $(gitpod workspace ssh <workspace-id> --dry-run) -t watch date`,
1928
RunE: func(cmd *cobra.Command, args []string) error {
2029
cmd.SilenceUsage = true
2130

@@ -26,7 +35,14 @@ var workspaceSSHCmd = &cobra.Command{
2635
return err
2736
}
2837

29-
return helper.SSHConnectToWorkspace(cmd.Context(), gitpod, workspaceID, dryRun)
38+
dashDashIndex := cmd.ArgsLenAtDash()
39+
40+
sshArgs := []string{}
41+
if dashDashIndex != -1 {
42+
sshArgs = args[dashDashIndex:]
43+
}
44+
45+
return helper.SSHConnectToWorkspace(cmd.Context(), gitpod, workspaceID, dryRun, sshArgs...)
3046
},
3147
}
3248

components/local-app/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ require (
8888
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
8989
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
9090
golang.org/x/net v0.17.0 // indirect
91-
golang.org/x/sys v0.13.0
91+
golang.org/x/sys v0.13.0 // indirect
9292
golang.org/x/text v0.13.0 // indirect
9393
google.golang.org/appengine v1.6.7 // indirect
9494
google.golang.org/genproto v0.0.0-20230913181813-007df8e322eb // indirect

components/local-app/pkg/helper/workspace.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func OpenWorkspaceInPreferredEditor(ctx context.Context, clnt *client.Gitpod, wo
9393
}
9494

9595
// SSHConnectToWorkspace connects to the workspace via SSH
96-
func SSHConnectToWorkspace(ctx context.Context, clnt *client.Gitpod, workspaceID string, runDry bool) error {
96+
func SSHConnectToWorkspace(ctx context.Context, clnt *client.Gitpod, workspaceID string, runDry bool, sshArgs ...string) error {
9797
workspace, err := clnt.Workspaces.GetWorkspace(ctx, connect.NewRequest(&v1.GetWorkspaceRequest{WorkspaceId: workspaceID}))
9898
if err != nil {
9999
return err
@@ -113,16 +113,20 @@ func SSHConnectToWorkspace(ctx context.Context, clnt *client.Gitpod, workspaceID
113113
ownerToken := token.Msg.Token
114114

115115
host := WorkspaceSSHHost(wsInfo)
116+
117+
command := exec.Command("ssh", fmt.Sprintf("%s#%s@%s", wsInfo.WorkspaceId, ownerToken, host), "-o", "StrictHostKeyChecking=no")
118+
if len(sshArgs) > 0 {
119+
slog.Debug("With additional SSH args and command", "with", sshArgs)
120+
command.Args = append(command.Args, "--", strings.Join(sshArgs, " "))
121+
}
116122
if runDry {
117-
fmt.Println("ssh", fmt.Sprintf("%s#%s@%s", wsInfo.WorkspaceId, ownerToken, host), "-o", "StrictHostKeyChecking=no")
123+
fmt.Println(strings.Join(command.Args, " "))
118124
return nil
119125
}
120-
121-
slog.Debug("Connecting to" + wsInfo.Description)
122-
command := exec.Command("ssh", fmt.Sprintf("%s#%s@%s", wsInfo.WorkspaceId, ownerToken, host), "-o", "StrictHostKeyChecking=no")
123126
command.Stdin = os.Stdin
124127
command.Stdout = os.Stdout
125128
command.Stderr = os.Stderr
129+
slog.Debug("Connecting to", "context", wsInfo.Description)
126130
err = command.Run()
127131
if err != nil {
128132
return err

0 commit comments

Comments
 (0)