Documentation ¶
Index ¶
- func NewCommand(h Handler, init Init) *std_cobra.Command
- func NewHandler(h Handler) *std_cobra.Command
- type Config
- func (c *Config) BindEnvToAllFlags(cmd *std_cobra.Command)
- func (c *Config) Init(envPrefix string, cmd *std_cobra.Command) *std_cobra.Command
- func (c *Config) KeyUsageString(key string) string
- func (c *Config) MissingRequiredKeyStrings() (missing []string)
- func (c *Config) PreRun() (showUsage bool, _ error)
- func (c *Config) RequiredKeyStrings() (required []string)
- func (c *Config) SetRequired(keys ...string)
- type Handler
- type Init
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewCommand ¶
NewCommand accepts an initial command object, created in a sub-commands Init method, and finishes preparation of the object.
For example, it automatically calls the handler's BindFlags method.
func NewHandler ¶
NewHandler is called by parent commands in order to create a new sub-command "defined" by the given handler.
The process of defining the sub-command relies on the handler implementing the Cobra interface from this package, e.g. Init method that provides the initial configuration elements like the command name, description, etc. within the cobra object.
subCmd := handler.New(&gc.Handler{}) parent.AddCommand(subCmd)
Types ¶
type Config ¶
Config provides viper integration and enforces prefixed environment variables.
It has a similar method set as cobra.Mixin implementations but is not a mixin. It is directly used in cobra.NewCommand to automatically add the functionality.
func (*Config) BindEnvToAllFlags ¶
BindEnvToAllFlags binds all flags in the command to the viper instance.
func (*Config) KeyUsageString ¶ added in v0.1.2
func (*Config) MissingRequiredKeyStrings ¶ added in v0.1.2
MissingRequiredKeyStrings returns a "--<flag key name>/<env key name>" element for each missing key.
func (*Config) PreRun ¶
PreRun checks for missing required flags, completes viper/cobra binding, etc.
It returns an error if any config is missing from both flags and environment. The error string contains a list of all missing config keys.
func (*Config) RequiredKeyStrings ¶ added in v0.1.2
RequiredKeyStrings returns a "--<flag key name>/<env key name>" element for each required key.
func (*Config) SetRequired ¶
SetRequired registers config keys which must be provided as a flag or environment value.
It panics if any key is invalid.
type Handler ¶
type Handler interface { handler.Session // BindFlags optionally defines CLI flags. BindFlags(*std_cobra.Command) (requiredFlags []string) // Init defines the cobra command object, prefix for environment variable configs, etc. Init() Init // Run is called when all bound flags are available. Run(context.Context, handler.Input) }
Cobra defines the handler behaviors implemented by each the Handler structs exported by each sub-command package.
It has several intents:
* Minimize boilerplate in sub-command packages. * Remove cobra knowledge from all Run methods for testability.