MCPcopy Create free account
hub / github.com/cli/cli / chooseCodespaceFromList

Function chooseCodespaceFromList

pkg/cmd/codespace/common.go:93–127  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 3

StopCodespaceMethod · 0.85
DeleteMethod · 0.85
chooseCodespaceMethod · 0.85

Calls 3

AskMethod · 0.95
ErrorfMethod · 0.65

Tested by

no test coverage detected