Documentation ¶
Index ¶
- Constants
- 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 ReadEnv(cfg interface{}) error
- func UpdateEnv(cfg interface{}) error
- func Usage(cfg interface{}, headerText *string, usageFuncs ...func()) func()
- type Setter
- type Updater
Constants ¶
View Source
const ( // Name of the environment variable or a list of names TagEnv = "env" // Value parsing layout (for types like time.Time) TagEnvLayout = "env-layout" // Default value TagEnvDefault = "env-default" // Custom list and map separator TagEnvSeparator = "env-separator" // Environment variable description TagEnvDescription = "env-description" // Flag to mark a field as updatable TagEnvUpd = "env-upd" // Flag to mark a field as required TagEnvRequired = "env-required" // 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 ¶
This section is empty.
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 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.