| 46 | } |
| 47 | |
| 48 | func SelectMap[T any](ask Asker, message string, items []T, getKey func(item T) string) (T, error) { |
| 49 | if util.Empty(items) { |
| 50 | return *new(T), fmt.Errorf("%s - no options available", message) |
| 51 | } |
| 52 | optionMap, options := MakeItemMapAndOptions(items, getKey) |
| 53 | var selectedValue T |
| 54 | var selectedKey string |
| 55 | if err := ask(&survey.Select{ |
| 56 | Message: message, |
| 57 | Options: options, |
| 58 | }, &selectedKey); err != nil { |
| 59 | return selectedValue, err |
| 60 | } |
| 61 | selectedValue, ok := optionMap[selectedKey] |
| 62 | if !ok { // without this explict check SelectMap can return nil, nil which people don't expect |
| 63 | return *new(T), fmt.Errorf("SelectMap did not get valid answer (selectedKey=%s)", selectedKey) |
| 64 | } |
| 65 | return selectedValue, nil |
| 66 | } |
| 67 | |
| 68 | func SelectMapWithNew[T any](ask Asker, message string, items []T, getKey func(item T) string) (T, bool, error) { |
| 69 | optionMap, options := MakeItemMapAndOptions(items, getKey) |