|
| 1 | +// Copyright (c) 2023 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License.AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +package cmd |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod" |
| 15 | + |
| 16 | + "github.com/spf13/cobra" |
| 17 | +) |
| 18 | + |
| 19 | +// todo(ft): distribute this common metadata from the root command to all subcommands to reduce latency |
| 20 | +var gitpodHost = os.Getenv("GITPOD_HOST") |
| 21 | + |
| 22 | +// sshCmd represents the ssh command |
| 23 | +var sshCmd = &cobra.Command{ |
| 24 | + Use: "ssh", |
| 25 | + Short: "Show the SSH connection command for the current workspace", |
| 26 | + Long: fmt.Sprintf(`Displays a command with which you can connect to the current workspace. |
| 27 | +The returned command requires SSH keys to be configured with Gitpod. |
| 28 | +See %s/user/keys for a guide on setting them up. |
| 29 | +`, gitpodHost), RunE: func(cmd *cobra.Command, args []string) error { |
| 30 | + ctx, cancel := context.WithTimeout(cmd.Context(), 10*time.Second) |
| 31 | + defer cancel() |
| 32 | + |
| 33 | + wsInfo, err := gitpod.GetWSInfo(ctx) |
| 34 | + if err != nil { |
| 35 | + return fmt.Errorf("cannot get workspace info: %w", err) |
| 36 | + } |
| 37 | + |
| 38 | + host := strings.Replace(wsInfo.WorkspaceUrl, wsInfo.WorkspaceId, wsInfo.WorkspaceId+".ssh", -1) |
| 39 | + sshKeyHost := fmt.Sprintf(`%s@%s`, wsInfo.WorkspaceId, host) |
| 40 | + |
| 41 | + sshCommand := fmt.Sprintf(`ssh '%s'`, sshKeyHost) |
| 42 | + fmt.Println(sshCommand) |
| 43 | + |
| 44 | + return nil |
| 45 | + }, |
| 46 | +} |
| 47 | + |
| 48 | +func init() { |
| 49 | + rootCmd.AddCommand(sshCmd) |
| 50 | +} |
0 commit comments