Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb0ee73

Browse files
committed
CLI
1 parent 1e5b776 commit fb0ee73

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

cli/cliui/parameter.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,16 @@ func RichParameter(cmd *cobra.Command, templateVersionParameter codersdk.Templat
7272
if len(templateVersionParameter.Options) > 0 {
7373
// Move the cursor up a single line for nicer display!
7474
_, _ = fmt.Fprint(cmd.OutOrStdout(), "\033[1A")
75-
value, err = Select(cmd, SelectOptions{
76-
Options: templateVersionParameterOptionValues(templateVersionParameter),
75+
var richParameterOption *codersdk.TemplateVersionParameterOption
76+
richParameterOption, err = RichSelect(cmd, RichSelectOptions{
77+
Options: templateVersionParameter.Options,
7778
Default: templateVersionParameter.DefaultValue,
7879
HideSearch: true,
7980
})
8081
if err == nil {
8182
_, _ = fmt.Fprintln(cmd.OutOrStdout())
82-
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+Styles.Prompt.String()+Styles.Field.Render(value))
83+
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+Styles.Prompt.String()+Styles.Field.Render(richParameterOption.Name))
84+
value = richParameterOption.Value
8385
}
8486
} else {
8587
text := "Enter a value"

cli/cliui/select.go

+39
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"github.com/AlecAivazis/survey/v2"
1010
"github.com/AlecAivazis/survey/v2/terminal"
1111
"github.com/spf13/cobra"
12+
"golang.org/x/xerrors"
13+
14+
"github.com/coder/coder/codersdk"
1215
)
1316

1417
func init() {
@@ -42,6 +45,42 @@ type SelectOptions struct {
4245
HideSearch bool
4346
}
4447

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+
4584
// Select displays a list of user options.
4685
func Select(cmd *cobra.Command, opts SelectOptions) (string, error) {
4786
// The survey library used *always* fails when testing on Windows,

cli/cliui/select_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/stretchr/testify/require"
1010

1111
"github.com/coder/coder/cli/cliui"
12+
"github.com/coder/coder/codersdk"
1213
"github.com/coder/coder/pty/ptytest"
1314
)
1415

@@ -42,3 +43,46 @@ func newSelect(ptty *ptytest.PTY, opts cliui.SelectOptions) (string, error) {
4243
cmd.SetIn(ptty.Input())
4344
return value, cmd.ExecuteContext(context.Background())
4445
}
46+
47+
func TestRichSelect(t *testing.T) {
48+
t.Parallel()
49+
t.Run("RichSelect", func(t *testing.T) {
50+
t.Parallel()
51+
ptty := ptytest.New(t)
52+
msgChan := make(chan string)
53+
go func() {
54+
resp, err := newRichSelect(ptty, cliui.RichSelectOptions{
55+
Options: []codersdk.TemplateVersionParameterOption{
56+
{
57+
Name: "A-Name",
58+
Value: "A-Value",
59+
Description: "A-Description",
60+
}, {
61+
Name: "B-Name",
62+
Value: "B-Value",
63+
Description: "B-Description",
64+
},
65+
},
66+
})
67+
assert.NoError(t, err)
68+
msgChan <- resp
69+
}()
70+
require.Equal(t, "A-Value", <-msgChan)
71+
})
72+
}
73+
74+
func newRichSelect(ptty *ptytest.PTY, opts cliui.RichSelectOptions) (string, error) {
75+
value := ""
76+
cmd := &cobra.Command{
77+
RunE: func(cmd *cobra.Command, args []string) error {
78+
richOption, err := cliui.RichSelect(cmd, opts)
79+
if err == nil {
80+
value = richOption.Value
81+
}
82+
return err
83+
},
84+
}
85+
cmd.SetOutput(ptty.Output())
86+
cmd.SetIn(ptty.Input())
87+
return value, cmd.ExecuteContext(context.Background())
88+
}

0 commit comments

Comments
 (0)