Skip to content

Commit c4354f0

Browse files
authored
local cli: support additional ssh arguments (#19046)
* local cli: support additional ssh arguments * address * 1 * add example
1 parent b2d5018 commit c4354f0

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ 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 with ssh command
23+
$ gitpod workspace ssh <workspace-id> -- ls -la
24+
$ gitpod ws ssh <workspace-id> -- -t watch date
25+
26+
# Get all SSH features with --dry-run
27+
$ $(gitpod workspace ssh <workspace-id> --dry-run) -- ls -la
28+
$ $(gitpod workspace ssh <workspace-id> --dry-run) -t watch date`,
1929
RunE: func(cmd *cobra.Command, args []string) error {
2030
cmd.SilenceUsage = true
2131

@@ -26,7 +36,14 @@ var workspaceSSHCmd = &cobra.Command{
2636
return err
2737
}
2838

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

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,13 +113,17 @@ 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, 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")
126+
slog.Debug("Connecting to", "context", wsInfo.Description)
123127
command.Stdin = os.Stdin
124128
command.Stdout = os.Stdout
125129
command.Stderr = os.Stderr

0 commit comments

Comments
 (0)