Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DebugLogger = func(lvl LogLevel, formatString string, a ...interface{}) { msg := fmt.Sprintf(formatString, a...) fmt.Printf("[%s] %s", lvl, msg) }
DebugLogger logs all messages of level debug or above
var ErrorLogger = func(lvl LogLevel, formatString string, a ...interface{}) { if lvl != LogLevel_Error { return } msg := fmt.Sprintf(formatString, a...) fmt.Printf("[%s] %s", lvl, msg) }
ErrorLogger logs all messages of level error
var InfoLogger = func(lvl LogLevel, formatString string, a ...interface{}) { if lvl == LogLevel_Debug { return } msg := fmt.Sprintf(formatString, a...) fmt.Printf("[%s] %s", lvl, msg) }
InfoLogger logs all messages of level info or above
var NoLogging = func(lvl LogLevel, formatString string, a ...interface{}) {
}
var WarnLogger = func(lvl LogLevel, formatString string, a ...interface{}) { if lvl == LogLevel_Debug || lvl == LogLevel_Info { return } msg := fmt.Sprintf(formatString, a...) fmt.Printf("[%s] %s", lvl, msg) }
WarnLogger logs all messages of level warn or above
Functions ¶
This section is empty.
Types ¶
type Entry ¶
type Entry struct {
// contains filtered or unexported fields
}
Entry is one item to define a configuration
func NewEntry ¶
func NewEntry(name, usage string, options ...EntryOption) Entry
NewEntry creates a new Entry that is available as flag, config file entry and environment variable
Example ¶
entry := NewEntry("port", "The port of the service", Default(8080), ShortName("p")) fmt.Printf("%s", entry)
Output: --port (-p) [default:8080 (int)] - The port of the service
func (Entry) DefaultValue ¶ added in v0.0.8
func (e Entry) DefaultValue() interface{}
DefaultValue provides the default value
type EntryOption ¶
type EntryOption func(e *Entry)
EntryOption represents an option for the Entry
func Bind ¶
func Bind(flag, env bool) EntryOption
Bind enables/ disables binding of flag and env var
func ShortName ¶
func ShortName(fShort string) EntryOption
ShortName specifies the shorthand (one-letter) flag name
type LoggerFunc ¶ added in v0.0.8
type Provider ¶
Provider is a structure containing the parsed configuration
func NewConfigProvider ¶ added in v0.0.8
func NewConfigProvider(target interface{}, configName, envPrefix string, options ...ProviderOption) (Provider, error)
NewConfigProvider creates a new config provider that is able to parse the command line, env vars and config file based on the given entries. This config provider automatically generates the needed config entries and fills the given config target based on the annotations on this struct. In case custom config entries should be used beside the annotations on the struct one can define them via
CustomConfigEntries(customEntries)`
e.g.
customEntries:=[]Entry{ // fill entries here } provider,err := NewConfigProvider(&myConfig,"my-config","MY_APP",CustomConfigEntries(customEntries))
Example ¶
// The configuration with the annotations needed in order to define how the config should be filled type myCfg struct { Field1 string `cfg:"{'name':'field-1','desc':'This is field 1','default':'default value for field 1'}"` Field2 int `cfg:"{'name':'field-2','desc':'This is field 2. It is a required field since no default values is defined.'}"` } cfg := myCfg{} // It is still possible to create entries manually and add them via CustomConfigEntries // But its value has then also filled into the config struct manually. manualConfigEntries := []Entry{NewEntry("manual", "Manually created flag")} // Create a provider based on the given config struct provider, err := NewConfigProvider(&cfg, "MyConfig", "MY_APP", Logger(WarnLogger), CustomConfigEntries(manualConfigEntries), ) if err != nil { panic(err) } args := []string{"--field-2=22"} // Read the parameters given via commandline into the config struct err = provider.ReadConfig(args) if err != nil { panic(err) } fmt.Print(provider.Usage())
Output: --manual (-) [required] env var: MY_APP_MANUAL default: n/a desc: Manually created flag --field-1 (-) env var: MY_APP_FIELD_1 default: default value for field 1 (type=string) desc: This is field 1 --field-2 (-) [required] env var: MY_APP_FIELD_2 default: n/a desc: This is field 2. It is a required field since no default values is defined.
func NewProvider ¶
func NewProvider(configEntries []Entry, configName, envPrefix string, options ...ProviderOption) Provider
NewProvider creates a new config provider that is able to parse the command line, env vars and config file based on the given entries
Example ¶
var configEntries []Entry configEntries = append(configEntries, NewEntry("port", "the port to listen to", Default(8080), ShortName("p"))) // no default value for this parameter --> thus it can be treated as a required one // to check if it was set by the user one can just call provider.IsSet("db-url") configEntries = append(configEntries, NewEntry("db-url", "the address of the data base")) configEntries = append(configEntries, NewEntry("db-reconnect", "enable automatic reconnect to the data base", Default(false))) provider := NewProvider(configEntries, "my-config", "MY_APP") args := []string{"--db-url=http://localhost"} err := provider.ReadConfig(args) if err != nil { panic(err) } port := provider.GetInt("port") // check for mandatory parameter if !provider.IsSet("db-url") { panic(fmt.Errorf("Parameter '--db-url' is missing")) } dbURL := provider.GetString("db-url") dbReconnect := provider.GetBool("db-reconnect") fmt.Printf("port=%d, dbURL=%s, dbReconnect=%t", port, dbURL, dbReconnect)
Output: port=8080, dbURL=http://localhost, dbReconnect=false
Example (WithConfigFile) ¶
var configEntries []Entry configEntries = append(configEntries, NewEntry("port", "the port to listen to", Default(8080), ShortName("p"))) provider := NewProvider(configEntries, "my-config", "MY_APP") args := []string{"--config-file=../test/data/config.yaml"} err := provider.ReadConfig(args) if err != nil { panic(err) } port := provider.GetInt("port") cfgFile := provider.GetString("config-file") fmt.Printf("port=%d was read from cfgFile=%s", port, cfgFile)
Output: port=12345 was read from cfgFile=../test/data/config.yaml
func (*Provider) ReadConfig ¶
ReadConfig parses commandline arguments, reads parameters from config and from environment
type ProviderOption ¶
type ProviderOption func(cfg *providerCfg)
ProviderOption represents an option for the Provider
func CfgFile ¶
func CfgFile(parameterName, shortParameterName string) ProviderOption
CfgFile specifies a default value
func CustomConfigEntries ¶ added in v0.0.8
func CustomConfigEntries(customConfigEntries []Entry) ProviderOption
CustomConfigEntries allows to add config entries that are created manually via NewEntry(..)
func Logger ¶ added in v0.0.8
func Logger(logger LoggerFunc) ProviderOption
Logger can be used to specify a custom logger