Skip to content

Commit

Permalink
local cli: support additional ssh arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mustard-mh committed Nov 8, 2023
1 parent df7929c commit 07576f2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
20 changes: 18 additions & 2 deletions components/local-app/cmd/workspace-ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ var dryRun bool
var workspaceSSHCmd = &cobra.Command{
Use: "ssh <workspace-id>",
Short: "Connects to a workspace via SSH",
Args: cobra.ExactArgs(1),
Args: cobra.MinimumNArgs(1),
Example: ` # connect to workspace with current terminal session
$ gitpod workspace ssh <workspace-id>
# Execute simple command in the workspace
$ gitpod workspace ssh <workspace-id> -- ls -la
# Get all SSH features with --dry-run
$ $(gitpod workspace ssh <workspace-id> --dry-run) -- ls -la
$ $(gitpod workspace ssh <workspace-id> --dry-run) -t watch date`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

Expand All @@ -26,7 +35,14 @@ var workspaceSSHCmd = &cobra.Command{
return err
}

return helper.SSHConnectToWorkspace(cmd.Context(), gitpod, workspaceID, dryRun)
dashDashIndex := cmd.ArgsLenAtDash()

sshArgs := []string{}
if dashDashIndex != -1 {
sshArgs = args[dashDashIndex:]
}

return helper.SSHConnectToWorkspace(cmd.Context(), gitpod, workspaceID, dryRun, sshArgs...)
},
}

Expand Down
2 changes: 1 addition & 1 deletion components/local-app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ require (
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230913181813-007df8e322eb // indirect
Expand Down
14 changes: 9 additions & 5 deletions components/local-app/pkg/helper/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func OpenWorkspaceInPreferredEditor(ctx context.Context, clnt *client.Gitpod, wo
}

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

host := WorkspaceSSHHost(wsInfo)

command := exec.Command("ssh", fmt.Sprintf("%s#%s@%s", wsInfo.WorkspaceId, ownerToken, host), "-o", "StrictHostKeyChecking=no")
if len(sshArgs) > 0 {
slog.Debug("With additional SSH args and command", "with", sshArgs)
command.Args = append(command.Args, "--", strings.Join(sshArgs, " "))
}
if runDry {
fmt.Println("ssh", fmt.Sprintf("%s#%s@%s", wsInfo.WorkspaceId, ownerToken, host), "-o", "StrictHostKeyChecking=no")
fmt.Println(strings.Join(command.Args, " "))
return nil
}

slog.Debug("Connecting to" + wsInfo.Description)
command := exec.Command("ssh", fmt.Sprintf("%s#%s@%s", wsInfo.WorkspaceId, ownerToken, host), "-o", "StrictHostKeyChecking=no")
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
slog.Debug("Connecting to", "context", wsInfo.Description)
err = command.Run()
if err != nil {
return err
Expand Down

0 comments on commit 07576f2

Please sign in to comment.