- Notifications
You must be signed in to change notification settings - Fork929
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
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
chore: add command to render all existing cli prompts
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commiteb9cfe1e8805576527ccb4be4e5797d8a1e7af0c
There are no files selected for viewing
1 change: 1 addition & 0 deletionscli/exp.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletionscli/prompts.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff 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(use string, prompt func(inv *serpent.Invocation) error, options ...serpent.Option) *serpent.Command { | ||
return &serpent.Command{ | ||
Use: use, | ||
Options: options, | ||
Handler: func(inv *serpent.Invocation) error { | ||
return prompt(inv) | ||
}, | ||
} | ||
} | ||
var useSearch bool | ||
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 { | ||
return inv.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) | ||
return err | ||
}), | ||
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(s string) error { | ||
if len(s) == 0 { | ||
return xerrors.Errorf("an input string is required") | ||
} | ||
if strings.ToUpper(string(s[0])) != string(s[0]) { | ||
return xerrors.Errorf("input string must start with a capital letter") | ||
} | ||
return nil | ||
}, | ||
}) | ||
_, _ = fmt.Fprintf(inv.Stdout, "%s\n", value) | ||
return err | ||
}), | ||
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(s string) error { | ||
if len(s) == 0 { | ||
return xerrors.Errorf("an input string is required") | ||
} | ||
return nil | ||
}, | ||
}) | ||
_, _ = fmt.Fprintf(inv.Stdout, "Your secret of length %d is safe with me\n", len(value)) | ||
return err | ||
}), | ||
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, | ||
}) | ||
if value == "Something else" { | ||
_, _ = fmt.Fprint(inv.Stdout, "I would have picked blue.\n") | ||
} else { | ||
_, _ = fmt.Fprintf(inv.Stdout, "%s is a nice color.\n", value) | ||
} | ||
return err | ||
}, 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, ", ")) | ||
return err | ||
}), | ||
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) | ||
return err | ||
}, useSearchOption), | ||
}, | ||
} | ||
return cmd | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.