Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commiteb9cfe1

Browse files
committed
chore: add command to render all existing cli prompts
1 parent089f068 commiteb9cfe1

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

‎cli/exp.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func (r *RootCmd) expCmd() *serpent.Command {
1313
Children: []*serpent.Command{
1414
r.scaletestCmd(),
1515
r.errorExample(),
16+
r.promptExample(),
1617
},
1718
}
1819
returncmd

‎cli/prompts.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"golang.org/x/xerrors"
8+
9+
"github.com/coder/coder/v2/cli/cliui"
10+
"github.com/coder/coder/v2/codersdk"
11+
"github.com/coder/serpent"
12+
)
13+
14+
func (RootCmd)promptExample()*serpent.Command {
15+
promptCmd:=func(usestring,promptfunc(inv*serpent.Invocation)error,options...serpent.Option)*serpent.Command {
16+
return&serpent.Command{
17+
Use:use,
18+
Options:options,
19+
Handler:func(inv*serpent.Invocation)error {
20+
returnprompt(inv)
21+
},
22+
}
23+
}
24+
25+
varuseSearchbool
26+
useSearchOption:= serpent.Option{
27+
Name:"search",
28+
Description:"Show the search.",
29+
Required:false,
30+
Flag:"search",
31+
Value:serpent.BoolOf(&useSearch),
32+
}
33+
cmd:=&serpent.Command{
34+
Use:"prompt-example",
35+
Short:"Example of various prompt types used within coder cli.",
36+
Long:"Example of various prompt types used within coder cli. "+
37+
"This command exists to aid in adjusting visuals of command prompts.",
38+
Handler:func(inv*serpent.Invocation)error {
39+
returninv.Command.HelpHandler(inv)
40+
},
41+
Children: []*serpent.Command{
42+
promptCmd("confirm",func(inv*serpent.Invocation)error {
43+
value,err:=cliui.Prompt(inv, cliui.PromptOptions{
44+
Text:"Basic confirmation prompt.",
45+
Default:"yes",
46+
IsConfirm:true,
47+
})
48+
_,_=fmt.Fprintf(inv.Stdout,"%s\n",value)
49+
returnerr
50+
}),
51+
promptCmd("validation",func(inv*serpent.Invocation)error {
52+
value,err:=cliui.Prompt(inv, cliui.PromptOptions{
53+
Text:"Input a string that starts with a capital letter.",
54+
Default:"",
55+
Secret:false,
56+
IsConfirm:false,
57+
Validate:func(sstring)error {
58+
iflen(s)==0 {
59+
returnxerrors.Errorf("an input string is required")
60+
}
61+
ifstrings.ToUpper(string(s[0]))!=string(s[0]) {
62+
returnxerrors.Errorf("input string must start with a capital letter")
63+
}
64+
returnnil
65+
},
66+
})
67+
_,_=fmt.Fprintf(inv.Stdout,"%s\n",value)
68+
returnerr
69+
}),
70+
promptCmd("secret",func(inv*serpent.Invocation)error {
71+
value,err:=cliui.Prompt(inv, cliui.PromptOptions{
72+
Text:"Input a secret",
73+
Default:"",
74+
Secret:true,
75+
IsConfirm:false,
76+
Validate:func(sstring)error {
77+
iflen(s)==0 {
78+
returnxerrors.Errorf("an input string is required")
79+
}
80+
returnnil
81+
},
82+
})
83+
_,_=fmt.Fprintf(inv.Stdout,"Your secret of length %d is safe with me\n",len(value))
84+
returnerr
85+
}),
86+
promptCmd("select",func(inv*serpent.Invocation)error {
87+
value,err:=cliui.Select(inv, cliui.SelectOptions{
88+
Options: []string{
89+
"Blue","Green","Yellow","Red","Something else",
90+
},
91+
Default:"",
92+
Message:"Select your favorite color:",
93+
Size:5,
94+
HideSearch:!useSearch,
95+
})
96+
ifvalue=="Something else" {
97+
_,_=fmt.Fprint(inv.Stdout,"I would have picked blue.\n")
98+
}else {
99+
_,_=fmt.Fprintf(inv.Stdout,"%s is a nice color.\n",value)
100+
}
101+
returnerr
102+
},useSearchOption),
103+
promptCmd("multi-select",func(inv*serpent.Invocation)error {
104+
values,err:=cliui.MultiSelect(inv, cliui.MultiSelectOptions{
105+
Message:"Select some things:",
106+
Options: []string{
107+
"Code","Chair","Whale","Diamond","Carrot",
108+
},
109+
Defaults: []string{"Code"},
110+
})
111+
_,_=fmt.Fprintf(inv.Stdout,"%q are nice choices.\n",strings.Join(values,", "))
112+
returnerr
113+
}),
114+
promptCmd("rich-parameter",func(inv*serpent.Invocation)error {
115+
value,err:=cliui.RichSelect(inv, cliui.RichSelectOptions{
116+
Options: []codersdk.TemplateVersionParameterOption{
117+
{
118+
Name:"Blue",
119+
Description:"Like the ocean.",
120+
Value:"blue",
121+
Icon:"/logo/blue.png",
122+
},
123+
{
124+
Name:"Red",
125+
Description:"Like a clown's nose.",
126+
Value:"red",
127+
Icon:"/logo/red.png",
128+
},
129+
{
130+
Name:"Yellow",
131+
Description:"Like a bumblebee. ",
132+
Value:"yellow",
133+
Icon:"/logo/yellow.png",
134+
},
135+
},
136+
Default:"blue",
137+
Size:5,
138+
HideSearch:useSearch,
139+
})
140+
_,_=fmt.Fprintf(inv.Stdout,"%s is a good choice.\n",value.Name)
141+
returnerr
142+
},useSearchOption),
143+
},
144+
}
145+
146+
returncmd
147+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp