Documentation ¶
Overview ¶
Package verdeter provides a config system for distributed programs
Index ¶
- Constants
- Variables
- func Key(cmd *cobra.Command, name string, valueType models.ConfigType, short string, ...) error
- type ConfigKey
- type VerdeterCommand
- func (verdeterCmd *VerdeterCommand) AddSubCommand(sub *VerdeterCommand)
- func (verdeterCmd *VerdeterCommand) AddValidator(name string, validator models.Validator)
- func (verdeterCmd *VerdeterCommand) Execute()
- func (verdeterCmd *VerdeterCommand) GKey(name string, valType models.ConfigType, short string, usage string) error
- func (verdeterCmd *VerdeterCommand) GetAppName() string
- func (verdeterCmd *VerdeterCommand) LKey(name string, valType models.ConfigType, short string, usage string) error
- func (verdeterCmd *VerdeterCommand) Lookup(configKeyName string) *ConfigKey
- func (verdeterCmd *VerdeterCommand) SetComputedValue(name string, computedValue models.DefaultValueFunction)
- func (verdeterCmd *VerdeterCommand) SetConstraint(msg string, constraint func() bool)
- func (verdeterCmd *VerdeterCommand) SetDefault(name string, value interface{})
- func (verdeterCmd *VerdeterCommand) SetNormalize(name string, normalize models.NormalizationFunction)
- func (verdeterCmd *VerdeterCommand) SetRequired(name string)
- func (verdeterCmd *VerdeterCommand) Validate(isTargetCommand bool) error
- type VerdeterConfig
Constants ¶
const ( IsStr models.ConfigType IsInt IsBool IsUint )
Variables ¶
var ErrConfigFileNotFound = errors.New("config file not found")
Functions ¶
func Key ¶
func Key(cmd *cobra.Command, name string, valueType models.ConfigType, short string, usage string, global bool) error
Key defines a flag in cobra bound to env and files
for booleans, the default value will be set to false for strings, the default value will be set to "" for floats, the default value will be set to 0
Types ¶
type ConfigKey ¶ added in v0.3.0
type ConfigKey struct { // the name of the config key Name string // contains filtered or unexported fields }
Represent a Config Key
func (*ConfigKey) CheckRequired ¶ added in v0.3.0
Check if the value is provided if the config key is required Return an error on failure
func (*ConfigKey) CheckValidators ¶ added in v0.3.0
Return an error on validation failure of one of the validator.
Return on first failure, the remaining validator are not ran.
func (*ConfigKey) ComputeDefaultValue ¶ added in v0.3.0
func (configKey *ConfigKey) ComputeDefaultValue()
Compute the default value of the config key using the DefaultValueFunction function if provided
type VerdeterCommand ¶
type VerdeterCommand struct {
// contains filtered or unexported fields
}
VerdeterCommand is a wrapper around github.com/spf13/cobra.Command and viper. It provides additional features such as:
- support for custom validators to make sure the input data is correct
- support for custom constraint functions (see models.ConstraintFunction)
- support for custom normalization function (see models.NormalizationFunction)
VerdeterCommand make the integration between cobra and viper possible.
func BuildVerdeterCommand ¶ added in v0.4.0
func BuildVerdeterCommand(config VerdeterConfig) *VerdeterCommand
BuildVerdeterCommand takes a VerdeterConfig and return a *VerdeterCommand
func NewVerdeterCommand ¶
func NewVerdeterCommand(use, shortDesc, longDesc string, runE func(verdeterCmd *VerdeterCommand, args []string) error) *VerdeterCommand
NewVerdeterCommand is the constructor for VerdeterCommand The args "use", "shortDesc" and "longDesc" are string, their role is the same as in github.com/spf13/cobra.Command The arg runE the callback for cobra
func (*VerdeterCommand) AddSubCommand ¶
func (verdeterCmd *VerdeterCommand) AddSubCommand(sub *VerdeterCommand)
Add a sub command
func (*VerdeterCommand) AddValidator ¶ added in v0.3.0
func (verdeterCmd *VerdeterCommand) AddValidator(name string, validator models.Validator)
Set the validator for a specific config key
func (*VerdeterCommand) Execute ¶
func (verdeterCmd *VerdeterCommand) Execute()
Execute the VerdeterCommand
(panics if called on a subcommand)
func (*VerdeterCommand) GKey ¶
func (verdeterCmd *VerdeterCommand) GKey(name string, valType models.ConfigType, short string, usage string) error
GKey defines a global flag for cobra bound to env and config file
func (*VerdeterCommand) GetAppName ¶
func (verdeterCmd *VerdeterCommand) GetAppName() string
GetAppName return the name of the root command
func (*VerdeterCommand) LKey ¶
func (verdeterCmd *VerdeterCommand) LKey(name string, valType models.ConfigType, short string, usage string) error
LKey defines a local flag for cobra bound to env and config file
func (*VerdeterCommand) Lookup ¶ added in v0.3.0
func (verdeterCmd *VerdeterCommand) Lookup(configKeyName string) *ConfigKey
Lookup returns the ConfigKey structure of the named config key, returning nil if none exists.
func (*VerdeterCommand) SetComputedValue ¶
func (verdeterCmd *VerdeterCommand) SetComputedValue(name string, computedValue models.DefaultValueFunction)
SetComputedValue sets a value dynamically as the default for a key
func (*VerdeterCommand) SetConstraint ¶
func (verdeterCmd *VerdeterCommand) SetConstraint(msg string, constraint func() bool)
SetConstraint sets a constraint
func (*VerdeterCommand) SetDefault ¶
func (verdeterCmd *VerdeterCommand) SetDefault(name string, value interface{})
SetDefault : set default value for a key
func (*VerdeterCommand) SetNormalize ¶
func (verdeterCmd *VerdeterCommand) SetNormalize(name string, normalize models.NormalizationFunction)
SetNormalize : function to normalize the value of a config Key (if set)
func (*VerdeterCommand) SetRequired ¶
func (verdeterCmd *VerdeterCommand) SetRequired(name string)
SetRequired sets a key as required
func (*VerdeterCommand) Validate ¶
func (verdeterCmd *VerdeterCommand) Validate(isTargetCommand bool) error
Validate checks if config keys have valid values
type VerdeterConfig ¶ added in v0.4.0
type VerdeterConfig struct { // Use is the one-line usage message. // Recommended syntax is as follow: // [ ] 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 Use string // Aliases is an array of aliases that can be used instead of the first word in Use. Aliases []string // SuggestFor is an array of command names for which this command will be suggested - // similar to aliases but only suggests. SuggestFor []string // 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 // ValidArgs is list of all valid non-flag arguments that are accepted in shell completions ValidArgs []string // ValidArgsFunction is an optional function that provides valid non-flag arguments for shell completion. // It is a dynamic version of using ValidArgs. // Only one of ValidArgs and ValidArgsFunction can be used for a command. ValidArgsFunction func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) // Expected arguments Args cobra.PositionalArgs // ArgAliases is List of aliases for ValidArgs. // These are not suggested to the user in the shell completion, // but accepted if entered manually. ArgAliases []string // BashCompletionFunction is custom bash functions used by the legacy bash autocompletion generator. // For portability with other shells, it is recommended to instead use ValidArgsFunction BashCompletionFunction string // Deprecated defines, if this command is deprecated and should print this string when used. Deprecated string // Annotations are key/value pairs that can be used by applications to identify or // group commands. Annotations map[string]string // Version defines the version for this command. If this value is non-empty and the command does not // define a "version" flag, a "version" boolean flag will be added to the command and, if specified, // will print content of the "Version" variable. A shorthand "v" flag will also be added if the // command does not define one. Version string // The *Run functions are executed in the following order: // * PersistentPreRun() // * PreRun() // * Run() // * PostRun() // * PersistentPostRun() // All functions get the same args, the arguments after the command name. // // PersistentPreRun: children of this command will inherit and execute. PersistentPreRun func(cmd *cobra.Command, args []string) // PersistentPreRunE: PersistentPreRun but returns an error. PersistentPreRunE func(cmd *cobra.Command, args []string) error // PreRun: children of this command will not inherit. PreRun func(cmd *cobra.Command, args []string) // PreRunE: PreRun but returns an error. PreRunE func(cmd *cobra.Command, args []string) error // Run: Typically the actual work function. Most commands will only implement this. Run func(cmd *cobra.Command, args []string) // RunE: Run but returns an error. RunE func(cmd *cobra.Command, args []string) error // PostRun: run after the Run command. PostRun func(cmd *cobra.Command, args []string) // PostRunE: PostRun but returns an error. PostRunE func(cmd *cobra.Command, args []string) error // PersistentPostRun: children of this command will inherit and execute after PostRun. PersistentPostRun func(cmd *cobra.Command, args []string) // PersistentPostRunE: PersistentPostRun but returns an error. PersistentPostRunE func(cmd *cobra.Command, args []string) error }
VerdeterConfig is meant to be passed to BuildVerdeterCommand() to return an initialized VerdeterCommand