package cmd
import (
"context"
"fmt"
"os"
"time"
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
"github.com/spf13/cobra"
)
var gitpodHost = os.Getenv("GITPOD_HOST")
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)
}
sshCommand := fmt.Sprintf("ssh '%s@%s.ssh.%s'", wsInfo.WorkspaceId, wsInfo.WorkspaceId, wsInfo.WorkspaceClusterHost)
fmt.Println(sshCommand)
return nil
},
}
func init() {
rootCmd.AddCommand(sshCmd)
}