Add some venom to make cobra and viper even more dangerous!
See _example folder for an example how to use venom. You can also use it as a skeleton for any 12-factor app you plan to use. It won't solve all problems but it will take care to solve the config one i.e. it will allow you to store your configuration in the environment variables.
The twist is that it doesn't require you to store all configuration in the environment variables. It is up to you to define how exactly you want to configure the application. It is even OK to mix however you want:
If you use TwelveFactorCmd
then here is the priority of resolution (highest to lowest):
$ example --foo 42
$ example -e EXAMPLE_FOO 42
$ example --env-file example.env # (assuming it has EXAMPLE_FOO=42 line)
$ EXAMPLE_ENV=EXAMPLE_FOO=42 ./bin/example
$ EXAMPLE_ENV_FILE=example.env ./bin/example
$ EXAMPLE_FOO=42 example
You probably should not use env as env trick as it is very confusing for any user.
It is possible to ask venom to define flags for you. You need to provide a struct or pointer to struct that has
special flag
tag set e.g.
type Config struct {
FooMoo int `flag:"foo-moo,m,Some mooness must be set"`
}
This will allow venom to find this tag and parse long flag name, short flag name and the description. It expect you to define it as a comma separated triplet. It has some logic to deduce what you meant in case you have use only one or two comma separated values.
To define flags you simply run DefineFlags
. Note that in this case all flags will have default values set to zero values
for their types i.e. 0 for int, "" for string, false for bool etc.:
flags := venom.DefineFlags(Config{})
RootCmd.PersistentFlags().AddFlagSet(flags)
You can not only define flags by a special struct but also the default values for these flags. Keep in mind that this works for simple cases (int, unit, string, bool) and probably fails for the rest. It implements only a very minimal subset of what pflags/cobra/viper are capable of - fix what you miss:
type Config struct {
FooMoo int `flag:"foo-moo,m,Some mooness must be set"`
}
To define default values simply override zero values:
defaults := Config {
FooMoo: 42,
}
flags := venom.DefineFlags(defaults)
RootCmd.PersistentFlags().AddFlagSet(flags)