Skip to content

gp ssh #17998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 22, 2023
Merged

gp ssh #17998

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions components/gitpod-cli/cmd/ssh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.

package cmd

import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"

"github.com/spf13/cobra"
)

// todo(ft): distribute this common metadata from the root command to all subcommands to reduce latency
var gitpodHost = os.Getenv("GITPOD_HOST")

// sshCmd represents the ssh command
var sshCmd = &cobra.Command{
Use: "ssh",
Short: "Show the SSH connection command for the current workspace",
Long: fmt.Sprintf(`Displays a command with which you can connect to the current workspace.
The returned command requires SSH keys to be configured with Gitpod.
See %s/user/keys for a guide on setting them up.
`, gitpodHost), RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(cmd.Context(), 10*time.Second)
defer cancel()

wsInfo, err := gitpod.GetWSInfo(ctx)
if err != nil {
return fmt.Errorf("cannot get workspace info: %w", err)
}

host := strings.Replace(wsInfo.WorkspaceUrl, wsInfo.WorkspaceId, wsInfo.WorkspaceId+".ssh", -1)
sshKeyHost := fmt.Sprintf(`%s@%s`, wsInfo.WorkspaceId, host)

sshCommand := fmt.Sprintf(`ssh '%s'`, sshKeyHost)
fmt.Println(sshCommand)

return nil
},
}

func init() {
rootCmd.AddCommand(sshCmd)
}