Documentation ¶
Index ¶
- Constants
- Variables
- func FUsage(w io.Writer, cfg interface{}, headerText *string, usageFuncs ...func()) func()
- func GetDescription(cfg interface{}, headerText *string) (string, error)
- func ReadConfig(path string, cfg interface{}) error
- func ReadConfigFromReader(reader io.Reader, ext ConfigType, cfg interface{}) error
- func ReadEnv(cfg interface{}) error
- func UpdateEnv(cfg interface{}) error
- func Usage(cfg interface{}, headerText *string, usageFuncs ...func()) func()
- type ConfigType
- type Setter
- type Updater
Constants ¶
View Source
const ( // TagEnv - name of the environment variable or a list of names. TagEnv = "env" // TagEnvLayout -value parsing layout (for types like time.Time). TagEnvLayout = "env-layout" // TagEnvDefault - default value. TagEnvDefault = "env-default" // TagEnvSeparator - custom list and map separator. TagEnvSeparator = "env-separator" // TagEnvDescription - environment variable description. TagEnvDescription = "env-description" // TagEnvUpd - flag to mark a field as updatable. TagEnvUpd = "env-upd" // TagEnvRequired - flag to mark a field as required. TagEnvRequired = "env-required" // TagEnvPrefix - flag to specify prefix for structure fields. TagEnvPrefix = "env-prefix" )
Supported tags.
View Source
const (
// DefaultSeparator is a default list and map separator character
DefaultSeparator = ","
)
Variables ¶
View Source
var ( ErrorFieldNotProvided = errors.New("field is required but the value is not provided") ErrorWrongTypeOfField = errors.New("wrong type of field") ErrorInvalidMapItem = errors.New("invalid map item") ErrorNotSupportedFileFormat = errors.New("this extation format doesn't supported by the parser") ErrorNotSupportedType = errors.New("this type of field doesn't supported by the parser") )
Functions ¶
func FUsage ¶
FUsage prints configuration help into the custom output. Other usage instructions can be wrapped in and executed before this usage function
func GetDescription ¶
GetDescription returns a description of environment variables. You can provide a custom header text.
func ReadConfig ¶
ReadConfig reads configuration file and parses it depending on tags in structure provided. Then it reads and parses
Example:
type ConfigDatabase struct { Port string `yaml:"port" env:"PORT" env-default:"5432"` Host string `yaml:"host" env:"HOST" env-default:"localhost"` Name string `yaml:"name" env:"NAME" env-default:"postgres"` User string `yaml:"user" env:"USER" env-default:"user"` Password string `yaml:"password" env:"PASSWORD"` } var cfg ConfigDatabase err := cleanenv.ReadConfig("config.yml", &cfg) if err != nil { ... }
func ReadConfigFromReader ¶
func ReadConfigFromReader(reader io.Reader, ext ConfigType, cfg interface{}) error
func ReadEnv ¶
func ReadEnv(cfg interface{}) error
ReadEnv reads environment variables into the structure.
Types ¶
type Setter ¶
Setter is an interface for a custom value setter.
To implement a custom value setter you need to add a SetValue function to your type that will receive a string raw value:
type MyField string func (f *MyField) SetValue(s string) error { if s == "" { return fmt.Errorf("field value can't be empty") } *f = MyField("my field is: " + s) return nil }
Click to show internal directories.
Click to hide internal directories.