Documentation ¶
Overview ¶
Package igconfig allows to set struct values based on different input places.
For example struct can be filled from default values defined in tags, environmental variables or Vault secrets.
All values in tags(except value in 'default' tag) should be lower-cased. Failing to make values lower-case will result in inability to properly set those fields. This is a choice to make lower number of mixed-cased values and generally be more in-line with best practices and proper usages.
Library is basically a set of "loaders".
Loader is anything that satisfies loader.Loader interface.
Such loaders will take application name and structure that should be filled and will do specific steps do try and set fields in structure.
LoadConfig function contains all the loaders that are enabled by default. This function should be used in most of the cases as only functionality that should be used by the caller.
In rare cases caller might want to use specific set of loaders to load configuration.
Some loaders also provide ability to periodically update values. This can be useful if application has logging and caller might want to change logging level at a runtime.
Example (FileLoader) ¶
Example_fileLoader for getting values from file(YAML or JSON). In this example, used to change etc path and get the file name from the appname.
package main import ( "fmt" "log" "time" "github.com/worldline-go/igconfig" "github.com/worldline-go/igconfig/loader" ) func main() { /* ===== YAML file of testdata/config/train.yml ===== speed: 10 ADDRESS: "Hoofddrop" bay: 4 secure: false info: train_name: "IN-NS1234" age: 10 destination: "Eindhoven" InfoStruct: train_name: "Embedded-NS1234" # WARNING RANDOM GETS IF SAME KEY EXISTS # testing | TesTing | tesTing are same for Testing | TesTing fields in struct testing: "testX" # TesTing: "testY" # tesTing: "testZ" # DoubleName: "textD" # doubleName: "textD" doublename: "textD" extra: new: "Extra data ignore" */ type InfoStruct struct { Name string `cfg:"train_name"` Age uint `cfg:"age"` Destination string Testing string TesTing string DoubleName string Time time.Time `cfg:"time" default:"2000-01-01T10:00:00Z"` } type AwesomeTrain struct { Name string `cfg:"train_name" env:"name" cmd:"name,n" default:"NS1"` Age uint `cfg:"age" env:"age" cmd:"age,a" default:"18"` Speed float64 `cfg:"speed" env:"speed" cmd:"speed,s" default:"200.50" loggable:"false"` Address string `cfg:"ADDRESS" env:"ADDRESS" default:"localhost"` Bay int `cmd:"bay,p" default:"10"` Secure bool `cfg:"secure" env:"secure" cmd:"secure" default:"false" loggable:"false"` Slice []string `cfg:"slice" env:"slice" cmd:"slice" default:"1,2,5,6"` InfoStruct Info InfoStruct InfoStructSkip InfoStruct `cfg:"-" default:"-"` } mytrain := AwesomeTrain{} // If not found, it return an error in igconfig loader. // os.Setenv(loader.EnvConfigFile, "testdata/config/train.yml") // defer os.Unsetenv(loader.EnvConfigFile) // err := igconfig.LoadConfig("train", &mytrain) // For this example just used file loader, most cases just use LoadConfig function. err := igconfig.LoadWithLoaders("train", &mytrain, loader.File{EtcPath: "testdata/config"}) if err != nil { log.Fatal("unable to load configuration settings", err) } fmt.Printf("%+v", mytrain) }
Output: {Name: Age:0 Speed:10 Address:Hoofddrop Bay:4 Secure:false Slice:[] InfoStruct:{Name:Embedded-NS1234 Age:0 Destination: Testing:testX TesTing:testX DoubleName:textD Time:0001-01-01 00:00:00 +0000 UTC} Info:{Name:IN-NS1234 Age:10 Destination:Eindhoven Testing: TesTing: DoubleName: Time:0001-01-01 00:00:00 +0000 UTC} InfoStructSkip:{Name: Age:0 Destination: Testing: TesTing: DoubleName: Time:0001-01-01 00:00:00 +0000 UTC}}
Index ¶
- Variables
- func ConfigureZerolog()
- func DefaultNameGetter(t reflect.StructField) string
- func LoadConfig(appName string, c interface{}) error
- func LoadConfigWithContext(ctx context.Context, appName string, c interface{}) error
- func LoadWithLoaders(appName string, configStruct interface{}, loaders ...loader.Loader) error
- func LoadWithLoadersWithContext(ctx context.Context, appName string, configStruct interface{}, ...) error
- type NameGetter
- type Printer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // LogTagOptionNames is a tag name for loggable boolean check. LogTagOptionNames = []string{"loggable", "log"} // SecretTagName is a tag name for secret loaders to prevent print it. SecretTagName = "secret" )
var DefaultLoaders = []loader.Loader{ &loader.Default{}, &loader.Consul{}, &loader.Vault{}, &loader.File{}, &loader.Env{}, &loader.Flags{}, }
DefaultLoaders is a list of default loaders to use.
Functions ¶
func ConfigureZerolog ¶
func ConfigureZerolog()
ConfigureZerolog can be used to "reset" default global logging configuration for zerolog.
This will update Time format to RFC3339Nano and will set up new global logger.
func DefaultNameGetter ¶
func DefaultNameGetter(t reflect.StructField) string
DefaultNameGetter returns lowercase field name as json field name.
func LoadConfig ¶
LoadConfig loads a configuration struct from loaders.
Example ¶
package main import ( "fmt" "log" "os" "github.com/rs/zerolog" "github.com/worldline-go/igconfig" "github.com/worldline-go/igconfig/testdata" ) func main() { var config testdata.TestConfig // Disable logging for unreachable local services. // In non-local environments this should not be done. zerolog.SetGlobalLevel(zerolog.ErrorLevel) // Below are just an examples of how values can be provided. You don't need to do this in your code. // In real-world - this will be provided from env, flags or Consul/Vault os.Args = []string{"executable", "-name", "FromFlags"} _ = os.Setenv("PORT", "5647") if err := igconfig.LoadConfig("adm0001s", &config); err != nil { log.Fatalf("load configuration: %s", err.Error()) } fmt.Println(config.Host) // This value is set from default fmt.Println(config.Name) // This value is set from application flags fmt.Println(config.Port) // This value is set from environmental variable }
Output: localhost FromFlags 5647
func LoadConfigWithContext ¶
LoadConfigWithContext loads a configuration struct from a fileName, the environment and finally from command-line parameters (the latter override the former) into a config struct. This is a convenience function encapsulating all individual loaders specified in DefaultLoaders.
func LoadWithLoaders ¶
LoadWithLoaders loads a configuration struct from a loaders.
Example ¶
package main import ( "fmt" "os" "github.com/worldline-go/igconfig" "github.com/worldline-go/igconfig/loader" "github.com/worldline-go/igconfig/testdata" ) func main() { // If only particular loaders are needed or new loader should be added - it is possible to do. // // igconfig.DefaultLoaders is an array of loaders provided by default. // // This example uses only Flags loader. // This means that no default or environmental variables will be loaded. // // Some loaders may accept additional configuration when used like this flagsLoader := loader.Flags{ NoUsage: true, } // Prepare pre-defined list of flags for this example os.Args = []string{"executable", "-salary", "12345.66"} var c testdata.TestConfig // igconfig.LoadWithLoaders provides ability to use specific loaders. // // P.S.: Please check errors in your code. _ = igconfig.LoadWithLoaders("adm0001s", &c, flagsLoader) fmt.Println(c.Name) fmt.Println(c.Salary) }
Output: 12345.66
Types ¶
type NameGetter ¶
type NameGetter func(t reflect.StructField) string
type Printer ¶
type Printer struct { // NameGetter will be called for each field to get name of it. NameGetter // Value is actual struct that should be printed. // It is possible for the value to be pointer to a struct. // // Value can implement some zerolog interfaces, but it has to be of proper type: // if interface is defined on pointer receiver - passed value should also be pointer. Value interface{} }
Printer is an implementation of zerolog.LogObjectMarshaler for marshaling struct to zerolog event.
Primary use case is to add configuration struct to the log.
Usage example:
conf := config.AppConfig{} // set up config value somehow // log is zerolog/log package log.Info(). Object("config", Printer{Value: conf}). Msg("loaded config")
func (Printer) MarshalZerologObject ¶
MarshalZerologObject marshals config to zerolog event.
Value should not be specifically config, but could be any struct.