Documentation ¶
Overview ¶
Package openvvar provides an easy way to manage flags and environment variables at same time. Making use of struct tags to structure your configurations, providing neat features like nested structs for correlated configurations, required fields, default values for all the "primitive" types, like ints, uints, strings, booleans, floats, time.Duration and slices for any of those types.
type DatabaseConfig struct { Name string `config:"name;default=postgresql"` Host string `config:"host;default=localhost"` Port int `config:"port;default=5432"` User string `config:"user;required"` Password string `config:"password;required"` } type Config struct { Database DatabaseConfig Debug bool `config:"debug;default=false;description=Set this config to true for debug log"` AcceptedHeroNames []string `config:"hero-names;default=Deadpool,Iron Man,Dr. Strange,Rocket Raccon"` UniversalAnswer uint8 `config:"universal-answer;default=42;short=u;description=THE ANSWER TO LIFE, THE UNIVERSE AND EVERYTHING"` SomeRandomFloat float64 `config:"random-float;default=149714.1241"` OneSecond time.Duration `config:"second;default=1s"` }
Nested fields have their parent field name concatenated to its own name
$ DATABASE_USER=root # For environment variables $ ./your_program -database-password=1234 # for flags
To load configurations, just instantiate an object from your struct and pass its pointer to Load function, checking for errors afterward:
configs := Config{} if err := openvvar.Load(&configs); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DotEnvNotFoundError ¶
type DotEnvNotFoundError struct {
Err error
}
DotEnvNotFoundError for when the file is not found
func (*DotEnvNotFoundError) Error ¶
func (e *DotEnvNotFoundError) Error() string
func (*DotEnvNotFoundError) Is ¶
func (e *DotEnvNotFoundError) Is(target error) bool
Is method to comply with new errors functions
func (*DotEnvNotFoundError) Unwrap ¶
func (e *DotEnvNotFoundError) Unwrap() error
type FlagCollectionError ¶
FlagCollectionError when we receive errors from flag library
func (*FlagCollectionError) Error ¶
func (e *FlagCollectionError) Error() string
func (*FlagCollectionError) Is ¶
func (e *FlagCollectionError) Is(target error) bool
Is method to comply with new errors functions
type FlagParseError ¶
type FlagParseError struct {
Err error
}
FlagParseError is for when we fail to parse a specific flag
func (*FlagParseError) Error ¶
func (e *FlagParseError) Error() string
func (*FlagParseError) Is ¶
func (e *FlagParseError) Is(target error) bool
Is method to comply with new errors functions
type InvalidReceiverError ¶
type InvalidReceiverError struct{}
InvalidReceiverError when developer pass something that isn't a point to struct to receive configs
func (*InvalidReceiverError) Error ¶
func (e *InvalidReceiverError) Error() string
func (*InvalidReceiverError) Is ¶
func (e *InvalidReceiverError) Is(target error) bool
Is method to comply with new errors functions
type InvalidTypeForDefaultValuesError ¶
type InvalidTypeForDefaultValuesError struct {
Type string
}
InvalidTypeForDefaultValuesError when developer puts a bogus default value for some type
func (*InvalidTypeForDefaultValuesError) Error ¶
func (e *InvalidTypeForDefaultValuesError) Error() string
func (*InvalidTypeForDefaultValuesError) Is ¶
func (e *InvalidTypeForDefaultValuesError) Is(target error) bool
Is method to comply with new errors functions
type MissingRequiredFieldError ¶
MissingRequiredFieldError when user forgets to fill a required config
func (*MissingRequiredFieldError) Error ¶
func (e *MissingRequiredFieldError) Error() string
func (*MissingRequiredFieldError) Is ¶
func (e *MissingRequiredFieldError) Is(target error) bool
Is method to comply with new errors functions
type TypeConversionError ¶
type TypeConversionError struct {
Err error
}
TypeConversionError occurs on string parsing for some types
func (*TypeConversionError) Error ¶
func (e *TypeConversionError) Error() string
func (*TypeConversionError) Is ¶
func (e *TypeConversionError) Is(target error) bool
Is method to comply with new errors functions
func (*TypeConversionError) Unwrap ¶
func (e *TypeConversionError) Unwrap() error
type ValueNotAValidOptionError ¶ added in v0.10.0
ValueNotAValidOptionError for when received valued is not listed as a valid option
func (*ValueNotAValidOptionError) Error ¶ added in v0.10.0
func (e *ValueNotAValidOptionError) Error() string
func (*ValueNotAValidOptionError) Is ¶ added in v0.10.0
func (e *ValueNotAValidOptionError) Is(target error) bool
Is method to comply with new errors functions