MCPcopy
hub / github.com/cli/cli / chooseCodespaceFromList

Function chooseCodespaceFromList

pkg/cmd/codespace/common.go:92–126  ·  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

90// chooseCodespaceFromList returns the codespace that the user has interactively selected from the list, or
91// an error if there are no codespaces.
92func 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
128func formatCodespacesForSelect(codespaces []*api.Codespace, includeOwner bool) []string {
129 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