Documentation
¶
Index ¶
- Variables
- func CobraCmdFromContext(ctx context.Context) *cobra.Command
- func ContextWithCobraCommand(ctx context.Context, cmd *cobra.Command) context.Context
- func ParseConfig(cfg Config, cmd *cobra.Command) error
- type Command
- type CommandWithAliases
- type CommandWithAliasesDecorator
- type CommandWithArgs
- type CommandWithArgsDecorator
- type CommandWithConfig
- type CommandWithConfigDecorator
- type CommandWithConfirm
- type CommandWithConfirmDecorator
- type CommandWithDeprecated
- type CommandWithDeprecatedDecorator
- type CommandWithDocs
- type CommandWithDocsDecorator
- type CommandWithExecute
- type CommandWithExecuteDecorator
- type CommandWithFlags
- type CommandWithFlagsDecorator
- type CommandWithHidden
- type CommandWithHiddenDecorator
- type CommandWithLogger
- type CommandWithLoggerDecorator
- type CommandWithOutput
- type CommandWithOutputDecorator
- type CommandWithPrompt
- type CommandWithPromptDecorator
- type CommandWithSubCommands
- type CommandWithSubCommandsDecorator
- type Config
- type Decorator
- type DefaultOutput
- type Docs
- type Ecdysis
- type Flag
- type Flags
- type Option
- type Output
Constants ¶
This section is empty.
Variables ¶
var DefaultDecorators = []Decorator{ CommandWithLoggerDecorator{}, CommandWithOutputDecorator{}, CommandWithAliasesDecorator{}, CommandWithFlagsDecorator{}, CommandWithConfigDecorator{}, CommandWithDocsDecorator{}, CommandWithHiddenDecorator{}, CommandWithSubCommandsDecorator{}, CommandWithDeprecatedDecorator{}, CommandWithArgsDecorator{}, CommandWithConfirmDecorator{}, CommandWithPromptDecorator{}, CommandWithExecuteDecorator{}, }
Functions ¶
func CobraCmdFromContext ¶ added in v0.1.0
CobraCmdFromContext fetches the cobra command from the context. If the context does not contain a cobra command, it returns nil.
func ContextWithCobraCommand ¶ added in v0.2.0
ContextWithCobraCommand provides the cobra command to the context. This is useful for situations such as wanting to execute cmd.Help() directly from Execute().
Types ¶
type Command ¶
type Command interface {
// Usage is the one-line usage message.
// Recommended syntax is as follows:
// [ ] identifies an optional argument. Arguments that are not enclosed in
// brackets are required.
// ... indicates that you can specify multiple values for the previous
// argument.
// | indicates mutually exclusive information. You can use the argument
// to the left of the separator or the argument to the right of the
// separator. You cannot use both arguments in a single use of the
// command.
// { } delimits a set of mutually exclusive arguments when one of the
// arguments is required. If the arguments are optional, they are
// enclosed in brackets ([ ]).
// Example: add [-F file | -D dir]... [-f format] profile
Usage() string
}
Command is an interface that represents a command that can be decorated and converted to a cobra.Command instance.
type CommandWithAliases ¶
type CommandWithAliases interface {
Command
// Aliases is a slice of aliases that can be used instead of the first word
// in Usage.
Aliases() []string
}
CommandWithAliases can be implemented by a command to provide aliases.
type CommandWithAliasesDecorator ¶
type CommandWithAliasesDecorator struct{}
CommandWithAliasesDecorator is a decorator that sets the command aliases.
type CommandWithArgs ¶
type CommandWithArgs interface {
Command
// Args is meant to parse arguments after the command name.
Args([]string) error
}
CommandWithArgs can be implemented by a command to parse arguments.
type CommandWithArgsDecorator ¶
type CommandWithArgsDecorator struct{}
CommandWithArgsDecorator is a decorator that provides the command arguments.
type CommandWithConfig ¶
CommandWithConfig can be implemented by a command to parsing configuration.
type CommandWithConfigDecorator ¶
type CommandWithConfigDecorator struct{}
CommandWithConfigDecorator is a decorator that sets the command flags.
type CommandWithConfirm ¶
type CommandWithConfirm interface {
Command
// ValueToConfirm adds a prompt before the command is executed where the
// user is asked to write the exact value as wantInput. If the user input
// matches the command will be executed, otherwise processing will be
// aborted.
ValueToConfirm(context.Context) (wantInput string)
}
CommandWithConfirm can be implemented by a command to require confirmation before execution. The user will be prompted to enter a specific value. If the value matches, the command will be executed, otherwise it will be aborted.
type CommandWithConfirmDecorator ¶
type CommandWithConfirmDecorator struct{}
CommandWithConfirmDecorator is a decorator that sets up a confirmation prompt before executing the command.
type CommandWithDeprecated ¶
type CommandWithDeprecated interface {
Command
// Deprecated returns a message that will be printed when the command is used.
Deprecated() string
}
CommandWithDeprecated can be implemented by a command to mark it as deprecated and print a message when it is used.
type CommandWithDeprecatedDecorator ¶
type CommandWithDeprecatedDecorator struct{}
CommandWithDeprecatedDecorator is a decorator that deprecates the command.
type CommandWithDocs ¶
type CommandWithDocs interface {
Command
// Docs returns the documentation for the command.
Docs() Docs
}
CommandWithDocs can be implemented by a command to provide documentation.
type CommandWithDocsDecorator ¶
type CommandWithDocsDecorator struct{}
CommandWithDocsDecorator is a decorator that sets the command documentation.
type CommandWithExecute ¶
type CommandWithExecute interface {
Command
// Execute is the actual work function. Most commands will implement this.
Execute(ctx context.Context) error
}
CommandWithExecute can be implemented by a command to provide an execution function.
type CommandWithExecuteDecorator ¶
type CommandWithExecuteDecorator struct{}
CommandWithExecuteDecorator is a decorator that sets the command execution.
type CommandWithFlags ¶
type CommandWithFlags interface {
Command
// Flags returns the set of flags on this command.
Flags() []Flag
}
CommandWithFlags can be implemented by a command to provide flags.
type CommandWithFlagsDecorator ¶
type CommandWithFlagsDecorator struct{}
CommandWithFlagsDecorator is a decorator that sets the command flags.
type CommandWithHidden ¶
type CommandWithHidden interface {
Command
// Hidden returns the desired hidden value for the command.
Hidden() bool
}
CommandWithHidden can be implemented by a command to hide it from the help.
type CommandWithHiddenDecorator ¶
type CommandWithHiddenDecorator struct{}
CommandWithHiddenDecorator is a decorator that sets the command hidden value.
type CommandWithLogger ¶
type CommandWithLogger interface {
Command
// Logger provides the logger to the command.
Logger(*slog.Logger)
}
CommandWithLogger can be implemented by a command to get a logger.
type CommandWithLoggerDecorator ¶
CommandWithLoggerDecorator is a decorator that provides a logger to the command. If the Logger field is not set, the default slog logger will be provided.
type CommandWithOutput ¶ added in v0.3.0
CommandWithOutput can be implemented by a command to provide its own stdout and stderr.
type CommandWithOutputDecorator ¶ added in v0.3.0
type CommandWithOutputDecorator struct {
Output Output
}
CommandWithOutputDecorator is a decorator that provides a Stdout to the command. If the Stdout field is not set, the default stdout will be provided.
type CommandWithPrompt ¶
type CommandWithPrompt interface {
Command
// Prompt adds a prompt before the command is executed where the user is
// asked to answer Y/N to proceed. It returns the message to be printed and
// a boolean indicating if the prompt was successfully processed.
Prompt() (message string, ok bool)
// SkipPrompt will return logic around when to skip prompt (e.g.: when all
// flags and arguments are specified).
SkipPrompt() bool
}
CommandWithPrompt can be implemented by a command to require confirmation before execution. The user will be prompted to answer Y/N to proceed.
type CommandWithPromptDecorator ¶
type CommandWithPromptDecorator struct{}
CommandWithPromptDecorator is a decorator that sets up a confirmation prompt before executing the command.
type CommandWithSubCommands ¶
type CommandWithSubCommands interface {
Command
// SubCommands defines subcommands of a command.
SubCommands() []Command
}
CommandWithSubCommands can be implemented by a command to provide subcommands.
type CommandWithSubCommandsDecorator ¶
type CommandWithSubCommandsDecorator struct{}
CommandWithSubCommandsDecorator is a decorator that sets the command subcommands.
type DefaultOutput ¶ added in v0.3.0
type DefaultOutput struct {
// contains filtered or unexported fields
}
func NewDefaultOutput ¶ added in v0.3.0
func NewDefaultOutput(cmd *cobra.Command) *DefaultOutput
func (*DefaultOutput) Output ¶ added in v0.3.0
func (d *DefaultOutput) Output(stdout, stderr io.Writer)
Output allows overwriting the stdout and/or stderr for specific use cases (like testing).
func (*DefaultOutput) Stderr ¶ added in v0.3.0
func (d *DefaultOutput) Stderr(msg any)
Stderr writes a message to the configured standard error.
func (*DefaultOutput) Stdout ¶ added in v0.3.0
func (d *DefaultOutput) Stdout(msg any)
Stdout writes a message to the configured standard output.
type Docs ¶
type Docs struct {
// Short is the short description shown in the 'help' output.
Short string
// Long is the long message shown in the 'help <this-command>' output.
Long string
// Example is examples of how to use the command.
Example string
}
Docs will be shown to the user when typing 'help' as well as in generated docs.
type Ecdysis ¶
type Ecdysis struct {
// Decorators is a list of decorators that are applied to all commands.
Decorators []Decorator
}
Ecdysis is the main struct that holds all decorators and is used to build cobra.Command instances from Command instances.
func New ¶
New creates a new Ecdysis instance with the provided options. By default, it uses the DefaultDecorators. Options can be used to add or replace decorators.
func (*Ecdysis) BuildCobraCommand ¶
BuildCobraCommand creates a new cobra.Command instance from the provided Command instance. It decorates the command with all registered decorators.
type Flag ¶
type Flag struct {
// Long name of the flag.
Long string
// Short name of the flag (one character).
Short string
// Usage is the description shown in the 'help' output.
Usage string
// Required is used to mark the flag as required.
Required bool
// Persistent is used to propagate the flag to subcommands.
Persistent bool
// Default is the default value when the flag is not explicitly supplied.
// It should have the same type as the value behind the pointer in field Ptr.
Default any
// Ptr is a pointer to the value into which the flag will be parsed.
Ptr any
// Hidden is used to mark the flag as hidden.
Hidden bool
}
Flag describes a single command line flag.
type Flags ¶
type Flags []Flag
func BuildFlags ¶
BuildFlags creates a slice of Flags from a struct. It supports nested structs and will only generate flags if it finds a 'short' or 'long' tag.
type Option ¶
type Option func(*Ecdysis)
Option is a function type that modifies an Ecdysis instance.
func WithDecorators ¶
WithDecorators adds or replaces a decorator of the same type.
func WithoutDefaultDecorators ¶
func WithoutDefaultDecorators() Option
WithoutDefaultDecorators removes all default decorators.