Documentation ¶
Overview ¶
Package configstruct provides a parse function to fill a config struct with values from cli flags or environment
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
Parse uses a given struct c with tags and parses values from env or cli flags, it uses the default FlagSet and os.Args
Example ¶
Example for using `configstruct` with default values.
// define a struct with tags type Config struct { Hostname string `env:"CONFIGSTRUCT_HOSTNAME" cli:"hostname" usage:"hostname value"` Port int `env:"CONFIGSTRUCT_PORT" cli:"port" usage:"listen port"` Debug bool `env:"CONFIGSTRUCT_DEBUG" cli:"debug" usage:"debug mode"` } // create a variable of the struct type and define defaults if needed conf := testConfig{ Hostname: "localhost", Port: 8000, Debug: true, } // now parse values from first cli flags and then env into this var err := Parse(&conf) if err != nil { fmt.Printf("can't parse config %s", err) }
Output:
Types ¶
type Command ¶ added in v1.3.0
type Command struct {
// contains filtered or unexported fields
}
Command defines a command that consists of a name (empty for root command), a struct that models all flags, a function that is executed if the command matches and that gets the config struct as argument several sub-commands can be added
func NewCommand ¶ added in v1.3.0
func NewCommand(name string, description string, config interface{}, f CommandFunc, subCommands ...*Command) *Command
NewCommand creates a command that is triggered by the given name in the command line all flags are defined by a struct that is parsed and filled with real values this struct is then set as argument for the function that is executed if the name matches
func (*Command) GetDependency ¶ added in v1.4.0
GetDependency gets a previous saved dependency in this command or any of the parent commands in the chain
func (*Command) ParseAndRun ¶ added in v1.3.0
ParseAndRun parses the given arguments and executes command functions
func (*Command) SetDependency ¶ added in v1.4.0
SetDependency saves a dependency referenced by a name for subcommands
type CommandFunc ¶ added in v1.4.0
CommandFunc is a function that is executed when a command is referenced in a CLI call
type Option ¶ added in v1.2.0
type Option func(c *config)
Option is a config setting function
func WithPrecedenceCli ¶ added in v1.2.0
func WithPrecedenceCli() Option
WithPrecedenceCli enabled precedence of cli over ENV values (default)
func WithPrecedenceEnv ¶ added in v1.2.0
func WithPrecedenceEnv() Option
WithPrecedenceEnv enabled precedence of ENV values over cli
func WithYamlConfig ¶ added in v1.6.0
WithYamlConfig sets the path to a yaml config file