From d910feb140b02854cecf05223c4ff00b5640ea14 Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Fri, 22 Sep 2023 15:13:49 +0300 Subject: [PATCH 1/3] ssh: Add options for port forwarding --- components/gitpod-cli/cmd/ssh.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/components/gitpod-cli/cmd/ssh.go b/components/gitpod-cli/cmd/ssh.go index 70a333e95ceab5..f3593618afbd01 100644 --- a/components/gitpod-cli/cmd/ssh.go +++ b/components/gitpod-cli/cmd/ssh.go @@ -18,6 +18,11 @@ import ( // todo(ft): distribute this common metadata from the root command to all subcommands to reduce latency var gitpodHost = os.Getenv("GITPOD_HOST") +var sshCmdOpts struct { + Local bool + Remote bool +} + // sshCmd represents the ssh command var sshCmd = &cobra.Command{ Use: "ssh", @@ -34,12 +39,22 @@ See %s/user/keys for a guide on setting them up. return fmt.Errorf("cannot get workspace info: %w", err) } - sshCommand := fmt.Sprintf("ssh '%s@%s.ssh.%s'", wsInfo.WorkspaceId, wsInfo.WorkspaceId, wsInfo.WorkspaceClusterHost) + var options := "" + if sshCmdOpts.Local { + options += "-L 3000:localhost:3000 " + } + if sshCmdOpts.Remote { + options += "-N -R 5000:localhost:5000 " + } + + sshCommand := fmt.Sprintf("ssh '%s@%s.ssh.%s' %s", wsInfo.WorkspaceId, wsInfo.WorkspaceId, wsInfo.WorkspaceClusterHost, options) fmt.Println(sshCommand) return nil }, } func init() { + sshCmd.Flags().BoolVarP(&sshCmdOpts.Local, "local", "", false, "Add options for local port forwarding") + sshCmd.Flags().BoolVarP(&sshCmdOpts.Remote, "remote", "", false, "Add options for remote port forwarding") rootCmd.AddCommand(sshCmd) } From c54b1b2dc5157a051f37d170bbb25699e966a7ef Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Fri, 22 Sep 2023 21:54:16 +0300 Subject: [PATCH 2/3] Smol mistake --- components/gitpod-cli/cmd/ssh.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/gitpod-cli/cmd/ssh.go b/components/gitpod-cli/cmd/ssh.go index f3593618afbd01..c35a912d7d0e2a 100644 --- a/components/gitpod-cli/cmd/ssh.go +++ b/components/gitpod-cli/cmd/ssh.go @@ -19,7 +19,7 @@ import ( var gitpodHost = os.Getenv("GITPOD_HOST") var sshCmdOpts struct { - Local bool + Local bool Remote bool } @@ -39,7 +39,7 @@ See %s/user/keys for a guide on setting them up. return fmt.Errorf("cannot get workspace info: %w", err) } - var options := "" + var options = "" if sshCmdOpts.Local { options += "-L 3000:localhost:3000 " } From dca8d4ec7cc65563121aac98b61c4383564ff80a Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Fri, 22 Sep 2023 22:23:13 +0300 Subject: [PATCH 3/3] Use slice to properly join strings --- components/gitpod-cli/cmd/ssh.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/components/gitpod-cli/cmd/ssh.go b/components/gitpod-cli/cmd/ssh.go index c35a912d7d0e2a..ff3055747f69a9 100644 --- a/components/gitpod-cli/cmd/ssh.go +++ b/components/gitpod-cli/cmd/ssh.go @@ -8,6 +8,7 @@ import ( "context" "fmt" "os" + "strings" "time" "github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod" @@ -39,16 +40,16 @@ See %s/user/keys for a guide on setting them up. return fmt.Errorf("cannot get workspace info: %w", err) } - var options = "" + var cmdline = []string{"ssh"} if sshCmdOpts.Local { - options += "-L 3000:localhost:3000 " + cmdline = append(cmdline, "-L 3000:localhost:3000") } if sshCmdOpts.Remote { - options += "-N -R 5000:localhost:5000 " + cmdline = append(cmdline, "-N -R 5000:localhost:5000") } - sshCommand := fmt.Sprintf("ssh '%s@%s.ssh.%s' %s", wsInfo.WorkspaceId, wsInfo.WorkspaceId, wsInfo.WorkspaceClusterHost, options) - fmt.Println(sshCommand) + cmdline = append(cmdline, fmt.Sprintf("'%s@%s.ssh.%s'", wsInfo.WorkspaceId, wsInfo.WorkspaceId, wsInfo.WorkspaceClusterHost)) + fmt.Println(strings.Join(cmdline, " ")) return nil }, }