RichSelect displays a list of user options including name and description.
(inv *serpent.Invocation, richOptions RichSelectOptions)
| 68 | |
| 69 | // RichSelect displays a list of user options including name and description. |
| 70 | func RichSelect(inv *serpent.Invocation, richOptions RichSelectOptions) (*codersdk.TemplateVersionParameterOption, error) { |
| 71 | opts := make([]string, len(richOptions.Options)) |
| 72 | var defaultOpt string |
| 73 | for i, option := range richOptions.Options { |
| 74 | line := option.Name |
| 75 | if len(option.Description) > 0 { |
| 76 | line += ": " + option.Description |
| 77 | } |
| 78 | opts[i] = line |
| 79 | |
| 80 | if option.Value == richOptions.Default { |
| 81 | defaultOpt = line |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | selected, err := Select(inv, SelectOptions{ |
| 86 | Options: opts, |
| 87 | Default: defaultOpt, |
| 88 | Size: richOptions.Size, |
| 89 | HideSearch: richOptions.HideSearch, |
| 90 | }) |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | |
| 95 | for i, option := range opts { |
| 96 | if option == selected { |
| 97 | return &richOptions.Options[i], nil |
| 98 | } |
| 99 | } |
| 100 | return nil, xerrors.Errorf("unknown option selected: %s", selected) |
| 101 | } |
| 102 | |
| 103 | // Select displays a list of user options. |
| 104 | func Select(inv *serpent.Invocation, opts SelectOptions) (string, error) { |