cmd/ghcs/ssh: Add -c parameter for specifying a Codespace to SSH to

- This adds a `-c`, `--codespace` parameter to `ghcs ssh` to allow for
  non-interactively specifying a Codespace to SSH into, for instance if
  a user has recently done `ghcs list` and already knows which Codespace
  they want to access. Without a value for the `-c` parameter, the
  interactive prompt appears as usual.
This commit is contained in:
Issy Long 2021-07-26 13:34:11 +01:00
parent d688dc76ec
commit c092a29350

View file

@ -18,19 +18,20 @@ import (
)
func NewSSHCmd() *cobra.Command {
var sshProfile string
var sshProfile, codespaceName string
var sshServerPort int
sshCmd := &cobra.Command{
Use: "ssh",
Short: "SSH into a GitHub Codespace, for use with running tests/editing in vim, etc.",
RunE: func(cmd *cobra.Command, args []string) error {
return SSH(sshProfile, sshServerPort)
return SSH(sshProfile, codespaceName, sshServerPort)
},
}
sshCmd.Flags().StringVarP(&sshProfile, "profile", "", "", "SSH Profile")
sshCmd.Flags().IntVarP(&sshServerPort, "server-port", "", 0, "SSH Server Port")
sshCmd.Flags().StringVarP(&codespaceName, "codespace", "c", "", "Codespace Name")
return sshCmd
}
@ -39,7 +40,7 @@ func init() {
rootCmd.AddCommand(NewSSHCmd())
}
func SSH(sshProfile string, sshServerPort int) error {
func SSH(sshProfile, codespaceName string, sshServerPort int) error {
apiClient := api.New(os.Getenv("GITHUB_TOKEN"))
ctx := context.Background()
@ -48,19 +49,37 @@ func SSH(sshProfile string, sshServerPort int) error {
return fmt.Errorf("error getting user: %v", err)
}
codespace, err := codespaces.ChooseCodespace(ctx, apiClient, user)
if err != nil {
if err == codespaces.ErrNoCodespaces {
fmt.Println(err.Error())
return nil
var (
codespace *api.Codespace
token string
)
if codespaceName == "" {
codespace, err = codespaces.ChooseCodespace(ctx, apiClient, user)
if err != nil {
if err == codespaces.ErrNoCodespaces {
fmt.Println(err.Error())
return nil
}
return fmt.Errorf("error choosing codespace: %v", err)
}
codespaceName = codespace.Name
token, err = apiClient.GetCodespaceToken(ctx, user.Login, codespaceName)
if err != nil {
return fmt.Errorf("error getting codespace token: %v", err)
}
} else {
token, err = apiClient.GetCodespaceToken(ctx, user.Login, codespaceName)
if err != nil {
return fmt.Errorf("error getting codespace token: %v", err)
}
return fmt.Errorf("error choosing codespace: %v", err)
}
token, err := apiClient.GetCodespaceToken(ctx, user.Login, codespace.Name)
if err != nil {
return fmt.Errorf("error getting codespace token: %v", err)
codespace, err = apiClient.GetCodespace(ctx, token, user.Login, codespaceName)
if err != nil {
return fmt.Errorf("error getting full codespace details: %v", err)
}
}
liveShareClient, err := codespaces.ConnectToLiveshare(ctx, apiClient, token, codespace)