MCPcopy Create free account
hub / github.com/OctopusDeploy/cli / SelectMapWithNew

Function SelectMapWithNew

pkg/question/select.go:68–86  ·  view source on GitHub ↗
(ask Asker, message string, items []T, getKey func(item T) string)

Source from the content-addressed store, hash-verified

66}
67
68func SelectMapWithNew[T any](ask Asker, message string, items []T, getKey func(item T) string) (T, bool, error) {
69 optionMap, options := MakeItemMapAndOptions(items, getKey)
70 var selectedValue T
71 var selectedKey string
72 if err := ask(&surveyext.Select{
73 Message: message,
74 Options: options,
75 }, &selectedKey); err != nil {
76 return selectedValue, false, err
77 }
78 if selectedKey == constants.PromptCreateNew {
79 return *new(T), true, nil
80 }
81 selectedValue, ok := optionMap[selectedKey]
82 if !ok { // without this explict check SelectMap can return nil, nil which people don't expect
83 return *new(T), false, fmt.Errorf("SelectMap did not get valid answer (selectedKey=%s)", selectedKey)
84 }
85 return selectedValue, false, nil
86}
87
88func MakeItemMapAndOptions[T any](items []T, getKey func(item T) string) (map[string]T, []string) {
89 optionMap := make(map[string]T, len(items))

Callers 1

SelectOrNewFunction · 0.92

Calls 1

MakeItemMapAndOptionsFunction · 0.85

Tested by

no test coverage detected