Documentation
¶
Overview ¶
Package console provides a high-level output and interaction API for CLI commands. It wraps a cobra.Command and renderer, providing structured output (tables, records, lists), interactive prompts (ask, confirm, choose, secret), feedback (spinner, progress), and markup-aware printing.
Usage:
c := console.New(cmd)
c.Render(vm) // template + markup → output
c.Print("<bold>hello</bold> world") // markup-aware print
name, _ := c.Ask("What is your name?")
ok, _ := c.Confirm("Continue?")
Index ¶
- Variables
- type Console
- func (c *Console) Alert(message string)
- func (c *Console) Ask(title string) (string, error)
- func (c *Console) AskWithPlaceholder(title, placeholder string) (string, error)
- func (c *Console) Choose(title string, options []Option) (string, error)
- func (c *Console) ChooseStrings(title string, choices []string) (string, error)
- func (c *Console) Confirm(title string) (bool, error)
- func (c *Console) Debug(msg string, keyvals ...any)
- func (c *Console) Dump(v ...any)
- func (c *Console) Error(msg string, keyvals ...any)
- func (c *Console) Fail(message string)
- func (c *Console) Format() renderer.Format
- func (c *Console) Info(message string)
- func (c *Console) Log(msg string, keyvals ...any)
- func (c *Console) Print(text string)
- func (c *Console) Printf(format string, args ...any)
- func (c *Console) Println(text string)
- func (c *Console) Progress(title string, total int) *progress.Bar
- func (c *Console) Render(data any) error
- func (c *Console) Secret(title string) (string, error)
- func (c *Console) Select(title string, options []Option, preselected []string) ([]string, error)
- func (c *Console) SelectStrings(title string, choices []string, preselected []string) ([]string, error)
- func (c *Console) Spinner(title string, action func()) error
- func (c *Console) Success(message string)
- func (c *Console) Warn(msg string, keyvals ...any)
- type Option
Constants ¶
This section is empty.
Variables ¶
var ErrNonInteractive = errors.New("prompt requires interactive mode (remove --non-interactive)")
ErrNonInteractive is returned when a prompt is called in non-interactive mode.
Functions ¶
This section is empty.
Types ¶
type Console ¶
type Console struct {
// contains filtered or unexported fields
}
Console wraps a cobra.Command with structured output and interaction methods.
func New ¶
New creates a Console from a cobra command. The renderer format is determined by the command's --format flag.
func (*Console) Ask ¶
Ask shows a text input prompt and returns the user's input. Returns ErrNonInteractive if --non-interactive is set.
func (*Console) AskWithPlaceholder ¶
AskWithPlaceholder shows a text input prompt with a placeholder hint. Returns ErrNonInteractive if --non-interactive is set.
func (*Console) Choose ¶
Choose shows a selection prompt and returns the chosen value. Returns ErrNonInteractive if --non-interactive is set.
func (*Console) ChooseStrings ¶
ChooseStrings is a convenience wrapper for Choose when labels and values are the same.
func (*Console) Confirm ¶
Confirm shows a yes/no confirmation prompt and returns the user's choice. Returns ErrNonInteractive if --non-interactive is set.
func (*Console) Dump ¶
Dump pretty-prints values to stderr for debugging. Output is always sent to stderr to avoid contaminating structured stdout.
func (*Console) Progress ¶
Progress returns a configured progress bar ready to run.
c.Progress("deploying", len(services)).Run(func(s *progress.Sender) error {
for i, svc := range services {
s.Step(i, svc.Name)
// ...
s.ItemDone(svc.Name)
}
s.Step(len(services), "")
return nil
})
func (*Console) Render ¶
Render delegates to the underlying renderer. For Templated viewmodels, this executes the template and applies markup post-processing. For JSON format, it marshals the struct.
func (*Console) Secret ¶
Secret shows a password input prompt (masked characters). Returns ErrNonInteractive if --non-interactive is set.
func (*Console) Select ¶
Select shows a multi-select prompt and returns the chosen values. preselected values are checked by default. Returns ErrNonInteractive if --non-interactive is set.
func (*Console) SelectStrings ¶
func (c *Console) SelectStrings(title string, choices []string, preselected []string) ([]string, error)
SelectStrings is a convenience wrapper for Select when labels and values are the same.