cli/cmd/ghcs/create.go
Issy Long b66d65379f cmd/ghcs/*.go: Better short descriptions of what commands do
- 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
```
2021-07-22 11:07:23 +01:00

146 lines
3.3 KiB
Go

package main
import (
"context"
"fmt"
"os"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/fatih/camelcase"
"github.com/github/ghcs/api"
"github.com/spf13/cobra"
)
var createCmd = &cobra.Command{
Use: "create",
Short: "Create a GitHub Codespace.",
RunE: func(cmd *cobra.Command, args []string) error {
return Create()
},
}
func init() {
rootCmd.AddCommand(createCmd)
}
var createSurvey = []*survey.Question{
{
Name: "repository",
Prompt: &survey.Input{Message: "Repository"},
Validate: survey.Required,
},
{
Name: "branch",
Prompt: &survey.Input{Message: "Branch"},
Validate: survey.Required,
},
}
func Create() error {
ctx := context.Background()
apiClient := api.New(os.Getenv("GITHUB_TOKEN"))
locationCh := getLocation(ctx, apiClient)
userCh := getUser(ctx, apiClient)
answers := struct {
Repository string
Branch string
}{}
if err := survey.Ask(createSurvey, &answers); err != nil {
return fmt.Errorf("error getting answers: %v", err)
}
repository, err := apiClient.GetRepository(ctx, answers.Repository)
if err != nil {
return fmt.Errorf("error getting repository: %v", err)
}
locationResult := <-locationCh
if locationResult.Err != nil {
return fmt.Errorf("error getting codespace region location: %v", locationResult.Err)
}
userResult := <-userCh
if userResult.Err != nil {
return fmt.Errorf("error getting codespace user: %v", userResult.Err)
}
skus, err := apiClient.GetCodespacesSkus(ctx, userResult.User, repository, locationResult.Location)
if err != nil {
return fmt.Errorf("error getting codespace skus: %v", err)
}
if len(skus) == 0 {
fmt.Println("There are no available machine types for this repository")
return nil
}
skuNames := make([]string, 0, len(skus))
skuByName := make(map[string]*api.Sku)
for _, sku := range skus {
nameParts := camelcase.Split(sku.Name)
machineName := strings.Title(strings.ToLower(nameParts[0]))
skuName := fmt.Sprintf("%s - %s", machineName, sku.DisplayName)
skuNames = append(skuNames, skuName)
skuByName[skuName] = sku
}
skuSurvey := []*survey.Question{
{
Name: "sku",
Prompt: &survey.Select{
Message: "Choose Machine Type:",
Options: skuNames,
Default: skuNames[0],
},
Validate: survey.Required,
},
}
skuAnswers := struct{ SKU string }{}
if err := survey.Ask(skuSurvey, &skuAnswers); err != nil {
return fmt.Errorf("error getting SKU: %v", err)
}
sku := skuByName[skuAnswers.SKU]
fmt.Println("Creating your codespace...")
codespace, err := apiClient.CreateCodespace(ctx, userResult.User, repository, sku, answers.Branch, locationResult.Location)
if err != nil {
return fmt.Errorf("error creating codespace: %v", err)
}
fmt.Println("Codespace created: " + codespace.Name)
return nil
}
type getUserResult struct {
User *api.User
Err error
}
func getUser(ctx context.Context, apiClient *api.API) <-chan getUserResult {
ch := make(chan getUserResult)
go func() {
user, err := apiClient.GetUser(ctx)
ch <- getUserResult{user, err}
}()
return ch
}
type locationResult struct {
Location string
Err error
}
func getLocation(ctx context.Context, apiClient *api.API) <-chan locationResult {
ch := make(chan locationResult)
go func() {
location, err := apiClient.GetCodespaceRegionLocation(ctx)
ch <- locationResult{location, err}
}()
return ch
}