chooseCodespaceFromList returns the codespace that the user has interactively selected from the list, or an error if there are no codespaces.
(ctx context.Context, codespaces []*api.Codespace, includeOwner bool, skipPromptForSingleOption bool)
| 90 | // chooseCodespaceFromList returns the codespace that the user has interactively selected from the list, or |
| 91 | // an error if there are no codespaces. |
| 92 | func chooseCodespaceFromList(ctx context.Context, codespaces []*api.Codespace, includeOwner bool, skipPromptForSingleOption bool) (*api.Codespace, error) { |
| 93 | if len(codespaces) == 0 { |
| 94 | return nil, errNoCodespaces |
| 95 | } |
| 96 | |
| 97 | if skipPromptForSingleOption && len(codespaces) == 1 { |
| 98 | return codespaces[0], nil |
| 99 | } |
| 100 | |
| 101 | sortedCodespaces := codespaces |
| 102 | sort.Slice(sortedCodespaces, func(i, j int) bool { |
| 103 | return sortedCodespaces[i].CreatedAt > sortedCodespaces[j].CreatedAt |
| 104 | }) |
| 105 | |
| 106 | csSurvey := []*survey.Question{ |
| 107 | { |
| 108 | Name: "codespace", |
| 109 | Prompt: &survey.Select{ |
| 110 | Message: "Choose codespace:", |
| 111 | Options: formatCodespacesForSelect(sortedCodespaces, includeOwner), |
| 112 | }, |
| 113 | Validate: survey.Required, |
| 114 | }, |
| 115 | } |
| 116 | |
| 117 | prompter := &Prompter{} |
| 118 | var answers struct { |
| 119 | Codespace int |
| 120 | } |
| 121 | if err := prompter.Ask(csSurvey, &answers); err != nil { |
| 122 | return nil, fmt.Errorf("error getting answers: %w", err) |
| 123 | } |
| 124 | |
| 125 | return sortedCodespaces[answers.Codespace], nil |
| 126 | } |
| 127 | |
| 128 | func formatCodespacesForSelect(codespaces []*api.Codespace, includeOwner bool) []string { |
| 129 | names := make([]string, len(codespaces)) |
no test coverage detected