|
| 1 | +package main |
| 2 | + |
| 3 | +// This file defines functions common to the entire ghcs command set. |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "sort" |
| 10 | + |
| 11 | + "github.com/AlecAivazis/survey/v2" |
| 12 | + "github.com/github/ghcs/api" |
| 13 | + "golang.org/x/term" |
| 14 | +) |
| 15 | + |
| 16 | +var errNoCodespaces = errors.New("You have no codespaces.") |
| 17 | + |
| 18 | +func chooseCodespace(ctx context.Context, apiClient *api.API, user *api.User) (*api.Codespace, error) { |
| 19 | + codespaces, err := apiClient.ListCodespaces(ctx, user) |
| 20 | + if err != nil { |
| 21 | + return nil, fmt.Errorf("error getting codespaces: %v", err) |
| 22 | + } |
| 23 | + |
| 24 | + if len(codespaces) == 0 { |
| 25 | + return nil, errNoCodespaces |
| 26 | + } |
| 27 | + |
| 28 | + sort.Slice(codespaces, func(i, j int) bool { |
| 29 | + return codespaces[i].CreatedAt > codespaces[j].CreatedAt |
| 30 | + }) |
| 31 | + |
| 32 | + codespacesByName := make(map[string]*api.Codespace) |
| 33 | + codespacesNames := make([]string, 0, len(codespaces)) |
| 34 | + for _, codespace := range codespaces { |
| 35 | + codespacesByName[codespace.Name] = codespace |
| 36 | + codespacesNames = append(codespacesNames, codespace.Name) |
| 37 | + } |
| 38 | + |
| 39 | + sshSurvey := []*survey.Question{ |
| 40 | + { |
| 41 | + Name: "codespace", |
| 42 | + Prompt: &survey.Select{ |
| 43 | + Message: "Choose codespace:", |
| 44 | + Options: codespacesNames, |
| 45 | + Default: codespacesNames[0], |
| 46 | + }, |
| 47 | + Validate: survey.Required, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + var answers struct { |
| 52 | + Codespace string |
| 53 | + } |
| 54 | + if err := ask(sshSurvey, &answers); err != nil { |
| 55 | + return nil, fmt.Errorf("error getting answers: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + codespace := codespacesByName[answers.Codespace] |
| 59 | + return codespace, nil |
| 60 | +} |
| 61 | + |
| 62 | +func getOrChooseCodespace(ctx context.Context, apiClient *api.API, user *api.User, codespaceName string) (codespace *api.Codespace, token string, err error) { |
| 63 | + if codespaceName == "" { |
| 64 | + codespace, err = chooseCodespace(ctx, apiClient, user) |
| 65 | + if err != nil { |
| 66 | + if err == errNoCodespaces { |
| 67 | + return nil, "", err |
| 68 | + } |
| 69 | + return nil, "", fmt.Errorf("choosing codespace: %v", err) |
| 70 | + } |
| 71 | + codespaceName = codespace.Name |
| 72 | + |
| 73 | + token, err = apiClient.GetCodespaceToken(ctx, user.Login, codespaceName) |
| 74 | + if err != nil { |
| 75 | + return nil, "", fmt.Errorf("getting codespace token: %v", err) |
| 76 | + } |
| 77 | + } else { |
| 78 | + token, err = apiClient.GetCodespaceToken(ctx, user.Login, codespaceName) |
| 79 | + if err != nil { |
| 80 | + return nil, "", fmt.Errorf("getting codespace token for given codespace: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + codespace, err = apiClient.GetCodespace(ctx, token, user.Login, codespaceName) |
| 84 | + if err != nil { |
| 85 | + return nil, "", fmt.Errorf("getting full codespace details: %v", err) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return codespace, token, nil |
| 90 | +} |
| 91 | + |
| 92 | +var hasTTY = term.IsTerminal(0) && term.IsTerminal(1) // is process connected to a terminal? |
| 93 | + |
| 94 | +// ask asks survey questions on the terminal, using standard options. |
| 95 | +// It fails unless hasTTY, but ideally callers should avoid calling it in that case. |
| 96 | +func ask(qs []*survey.Question, response interface{}) error { |
| 97 | + if !hasTTY { |
| 98 | + return fmt.Errorf("no terminal") |
| 99 | + } |
| 100 | + return survey.Ask(qs, response, survey.WithShowCursor(true)) |
| 101 | +} |
0 commit comments