Skip to content

Commit bad2fe2

Browse files
committed
feat: CLI use multiselect for list(string)
1 parent a7c734c commit bad2fe2

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

cli/cliui/parameter.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cliui
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"strings"
67

@@ -69,7 +70,28 @@ func RichParameter(cmd *cobra.Command, templateVersionParameter codersdk.Templat
6970

7071
var err error
7172
var value string
72-
if len(templateVersionParameter.Options) > 0 {
73+
if templateVersionParameter.Type == "list(string)" {
74+
// Move the cursor up a single line for nicer display!
75+
_, _ = fmt.Fprint(cmd.OutOrStdout(), "\033[1A")
76+
77+
var options []string
78+
err = json.Unmarshal([]byte(templateVersionParameter.DefaultValue), &options)
79+
if err != nil {
80+
return "", err
81+
}
82+
83+
values, err := MultiSelect(cmd, options)
84+
if err == nil {
85+
v, err := json.Marshal(&values)
86+
if err != nil {
87+
return "", err
88+
}
89+
90+
_, _ = fmt.Fprintln(cmd.OutOrStdout())
91+
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+Styles.Prompt.String()+Styles.Field.Render(strings.Join(values, ", ")))
92+
value = string(v)
93+
}
94+
} else if len(templateVersionParameter.Options) > 0 {
7395
// Move the cursor up a single line for nicer display!
7496
_, _ = fmt.Fprint(cmd.OutOrStdout(), "\033[1A")
7597
var richParameterOption *codersdk.TemplateVersionParameterOption

cli/cliui/select.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ func init() {
3535
{{- template "option" $.IterateOption $ix $option}}
3636
{{- end}}
3737
{{- end }}`
38+
39+
survey.MultiSelectQuestionTemplate = `
40+
{{- define "option"}}
41+
{{- if eq .SelectedIndex .CurrentIndex }}{{color .Config.Icons.SelectFocus.Format }}{{ .Config.Icons.SelectFocus.Text }}{{color "reset"}}{{else}} {{end}}
42+
{{- if index .Checked .CurrentOpt.Index }}{{color .Config.Icons.MarkedOption.Format }} {{ .Config.Icons.MarkedOption.Text }} {{else}}{{color .Config.Icons.UnmarkedOption.Format }} {{ .Config.Icons.UnmarkedOption.Text }} {{end}}
43+
{{- color "reset"}}
44+
{{- " "}}{{- .CurrentOpt.Value}}
45+
{{end}}
46+
{{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
47+
{{- if not .ShowAnswer }}
48+
{{- "\n"}}
49+
{{- range $ix, $option := .PageEntries}}
50+
{{- template "option" $.IterateOption $ix $option}}
51+
{{- end}}
52+
{{- end}}`
3853
}
3954

4055
type SelectOptions struct {
@@ -118,6 +133,30 @@ func Select(cmd *cobra.Command, opts SelectOptions) (string, error) {
118133
return value, err
119134
}
120135

136+
func MultiSelect(cmd *cobra.Command, items []string) ([]string, error) {
137+
// Similar hack is applied to Select()
138+
if flag.Lookup("test.v") != nil {
139+
return items, nil
140+
}
141+
142+
prompt := &survey.MultiSelect{
143+
Message: "Options:",
144+
Options: items,
145+
Default: items,
146+
}
147+
148+
var values []string
149+
err := survey.AskOne(prompt, &values, survey.WithStdio(fileReadWriter{
150+
Reader: cmd.InOrStdin(),
151+
}, fileReadWriter{
152+
Writer: cmd.OutOrStdout(),
153+
}, cmd.OutOrStdout()))
154+
if errors.Is(err, terminal.InterruptErr) {
155+
return nil, Canceled
156+
}
157+
return values, err
158+
}
159+
121160
type fileReadWriter struct {
122161
io.Reader
123162
io.Writer

0 commit comments

Comments
 (0)