cobra

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2020 License: MPL-2.0, MPL-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCommand

func NewCommand(h Handler, init Init) *std_cobra.Command

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

func NewHandler(h Handler) *std_cobra.Command

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

type Config struct {
	*viper.Viper
	// contains filtered or unexported fields
}

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

func (c *Config) BindEnvToAllFlags(cmd *std_cobra.Command)

BindEnvToAllFlags binds all flags in the command to the viper instance.

func (*Config) Init

func (c *Config) Init(envPrefix string, cmd *std_cobra.Command) *std_cobra.Command

Init creates the config storage instance.

func (*Config) KeyUsageString added in v0.1.1

func (c *Config) KeyUsageString(key string) string

func (*Config) MissingRequiredKeyStrings added in v0.1.1

func (c *Config) MissingRequiredKeyStrings() (missing []string)

MissingRequiredKeyStrings returns a "--<flag key name>/<env key name>" element for each missing key.

func (*Config) PreRun

func (c *Config) PreRun() (showUsage bool, _ error)

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.1

func (c *Config) RequiredKeyStrings() (required []string)

RequiredKeyStrings returns a "--<flag key name>/<env key name>" element for each required key.

func (*Config) SetRequired

func (c *Config) SetRequired(keys ...string)

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.

type Init

type Init struct {
	Cmd *std_cobra.Command

	Ctx context.Context

	EnvPrefix string

	// Mixins defines all Mixin implementations for automatic integration into stages
	// of the command run, e.g. binding flags.
	Mixins []handler.Mixin
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL