|
9 | 9 | "github.com/AlecAivazis/survey/v2"
|
10 | 10 | "github.com/AlecAivazis/survey/v2/terminal"
|
11 | 11 | "github.com/spf13/cobra"
|
| 12 | + "golang.org/x/xerrors" |
| 13 | + |
| 14 | + "github.com/coder/coder/codersdk" |
12 | 15 | )
|
13 | 16 |
|
14 | 17 | func init() {
|
@@ -42,6 +45,42 @@ type SelectOptions struct {
|
42 | 45 | HideSearch bool
|
43 | 46 | }
|
44 | 47 |
|
| 48 | +type RichSelectOptions struct { |
| 49 | + Options []codersdk.TemplateVersionParameterOption |
| 50 | + Default string |
| 51 | + Size int |
| 52 | + HideSearch bool |
| 53 | +} |
| 54 | + |
| 55 | +// RichSelect displays a list of user options including name and description. |
| 56 | +func RichSelect(cmd *cobra.Command, richOptions RichSelectOptions) (*codersdk.TemplateVersionParameterOption, error) { |
| 57 | + opts := make([]string, len(richOptions.Options)) |
| 58 | + for i, option := range richOptions.Options { |
| 59 | + line := option.Name |
| 60 | + if len(option.Description) > 0 { |
| 61 | + line += ": " + option.Description |
| 62 | + } |
| 63 | + opts[i] = line |
| 64 | + } |
| 65 | + |
| 66 | + selected, err := Select(cmd, SelectOptions{ |
| 67 | + Options: opts, |
| 68 | + Default: richOptions.Default, |
| 69 | + Size: richOptions.Size, |
| 70 | + HideSearch: richOptions.HideSearch, |
| 71 | + }) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + for i, option := range opts { |
| 77 | + if option == selected { |
| 78 | + return &richOptions.Options[i], nil |
| 79 | + } |
| 80 | + } |
| 81 | + return nil, xerrors.Errorf("unknown option selected: %s", selected) |
| 82 | +} |
| 83 | + |
45 | 84 | // Select displays a list of user options.
|
46 | 85 | func Select(cmd *cobra.Command, opts SelectOptions) (string, error) {
|
47 | 86 | // The survey library used *always* fails when testing on Windows,
|
|
0 commit comments