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

chore: add command to render all existing cli prompts#13689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Emyrk merged 1 commit intomainfromstevenmasley/survey_prompt
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletionscli/exp.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@ func (r *RootCmd) expCmd() *serpent.Command {
Children: []*serpent.Command{
r.scaletestCmd(),
r.errorExample(),
r.promptExample(),
},
}
returncmd
Expand Down
147 changes: 147 additions & 0 deletionscli/prompts.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
package cli

import (
"fmt"
"strings"

"golang.org/x/xerrors"

"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/serpent"
)

func (RootCmd)promptExample()*serpent.Command {
promptCmd:=func(usestring,promptfunc(inv*serpent.Invocation)error,options...serpent.Option)*serpent.Command {
return&serpent.Command{
Use:use,
Options:options,
Handler:func(inv*serpent.Invocation)error {
returnprompt(inv)
},
}
}

varuseSearchbool
useSearchOption:= serpent.Option{
Name:"search",
Description:"Show the search.",
Required:false,
Flag:"search",
Value:serpent.BoolOf(&useSearch),
}
cmd:=&serpent.Command{
Use:"prompt-example",
Short:"Example of various prompt types used within coder cli.",
Long:"Example of various prompt types used within coder cli. "+
"This command exists to aid in adjusting visuals of command prompts.",
Handler:func(inv*serpent.Invocation)error {
returninv.Command.HelpHandler(inv)
},
Children: []*serpent.Command{
promptCmd("confirm",func(inv*serpent.Invocation)error {
value,err:=cliui.Prompt(inv, cliui.PromptOptions{
Text:"Basic confirmation prompt.",
Default:"yes",
IsConfirm:true,
})
_,_=fmt.Fprintf(inv.Stdout,"%s\n",value)
returnerr
}),
promptCmd("validation",func(inv*serpent.Invocation)error {
value,err:=cliui.Prompt(inv, cliui.PromptOptions{
Text:"Input a string that starts with a capital letter.",
Default:"",
Secret:false,
IsConfirm:false,
Validate:func(sstring)error {
iflen(s)==0 {
returnxerrors.Errorf("an input string is required")
}
ifstrings.ToUpper(string(s[0]))!=string(s[0]) {
returnxerrors.Errorf("input string must start with a capital letter")
}
returnnil
},
})
_,_=fmt.Fprintf(inv.Stdout,"%s\n",value)
returnerr
}),
promptCmd("secret",func(inv*serpent.Invocation)error {
value,err:=cliui.Prompt(inv, cliui.PromptOptions{
Text:"Input a secret",
Default:"",
Secret:true,
IsConfirm:false,
Validate:func(sstring)error {
iflen(s)==0 {
returnxerrors.Errorf("an input string is required")
}
returnnil
},
})
_,_=fmt.Fprintf(inv.Stdout,"Your secret of length %d is safe with me\n",len(value))
returnerr
}),
promptCmd("select",func(inv*serpent.Invocation)error {
value,err:=cliui.Select(inv, cliui.SelectOptions{
Options: []string{
"Blue","Green","Yellow","Red","Something else",
},
Default:"",
Message:"Select your favorite color:",
Size:5,
HideSearch:!useSearch,
})
ifvalue=="Something else" {
_,_=fmt.Fprint(inv.Stdout,"I would have picked blue.\n")
}else {
_,_=fmt.Fprintf(inv.Stdout,"%s is a nice color.\n",value)
}
returnerr
},useSearchOption),
promptCmd("multi-select",func(inv*serpent.Invocation)error {
values,err:=cliui.MultiSelect(inv, cliui.MultiSelectOptions{
Message:"Select some things:",
Options: []string{
"Code","Chair","Whale","Diamond","Carrot",
},
Defaults: []string{"Code"},
})
_,_=fmt.Fprintf(inv.Stdout,"%q are nice choices.\n",strings.Join(values,", "))
returnerr
}),
promptCmd("rich-parameter",func(inv*serpent.Invocation)error {
value,err:=cliui.RichSelect(inv, cliui.RichSelectOptions{
Options: []codersdk.TemplateVersionParameterOption{
{
Name:"Blue",
Description:"Like the ocean.",
Value:"blue",
Icon:"/logo/blue.png",
},
{
Name:"Red",
Description:"Like a clown's nose.",
Value:"red",
Icon:"/logo/red.png",
},
{
Name:"Yellow",
Description:"Like a bumblebee. ",
Value:"yellow",
Icon:"/logo/yellow.png",
},
},
Default:"blue",
Size:5,
HideSearch:useSearch,
})
_,_=fmt.Fprintf(inv.Stdout,"%s is a good choice.\n",value.Name)
returnerr
},useSearchOption),
},
}

returncmd
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp