- I ran `--help` on `ghcs code` and saw `ghcs code` and that was it,
which was surprising. I expected a description.
- Here's a fix for all of the commands thus far to give them longer
descriptions.
- I've only done "short" descriptions in Cobra terms, and removed the
"long" descriptions as they seemed like they needed to be
unnecessarily verbose.
Before:
```
❯ ghcs --help
Codespaces
Usage:
ghcs [command]
Available Commands:
code code
create Create
delete delete
help Help about any command
list list
ports ports
ssh ssh
Flags:
-h, --help help for ghcs
-v, --version version for ghcs
Use "ghcs [command] --help" for more information about a command.
❯ ghcs ssh --help
ssh
Usage:
ghcs ssh [flags]
Flags:
-h, --help help for ssh
--profile string SSH Profile
--server-port int SSH Server Port
```
After:
```
❯ ./ghcs --help
Codespaces
Usage:
ghcs [command]
Available Commands:
code Open a GitHub Codespace in VSCode.
create Create a GitHub Codespace.
delete Delete a GitHub Codespace.
help Help about any command
list List GitHub Codespaces you have on your account.
ports Forward ports from a GitHub Codespace.
ssh SSH into a GitHub Codespace, for use with running tests/editing in vim, etc.
Flags:
-h, --help help for ghcs
-v, --version version for ghcs
Use "ghcs [command] --help" for more information about a command.
❯ ./ghcs ssh --help
SSH into a GitHub Codespace, for use with running tests/editing in vim, etc.
Usage:
ghcs ssh [flags]
Flags:
-h, --help help for ssh
--profile string SSH Profile
--server-port int SSH Server Port
```
63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
|
|
"github.com/github/ghcs/api"
|
|
"github.com/github/ghcs/internal/codespaces"
|
|
"github.com/skratchdot/open-golang/open"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func NewCodeCmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "code",
|
|
Short: "Open a GitHub Codespace in VSCode.",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
var codespaceName string
|
|
if len(args) > 0 {
|
|
codespaceName = args[0]
|
|
}
|
|
return Code(codespaceName)
|
|
},
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(NewCodeCmd())
|
|
}
|
|
|
|
func Code(codespaceName string) error {
|
|
apiClient := api.New(os.Getenv("GITHUB_TOKEN"))
|
|
ctx := context.Background()
|
|
|
|
user, err := apiClient.GetUser(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("error getting user: %v", err)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
if err := open.Run(vscodeProtocolURL(codespaceName)); err != nil {
|
|
return fmt.Errorf("error opening vscode URL")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func vscodeProtocolURL(codespaceName string) string {
|
|
return fmt.Sprintf("vscode://github.codespaces/connect?name=%s", url.QueryEscape(codespaceName))
|
|
}
|