package
0.4.0
Repository: https://github.com/puppetlabs/go-libs.git
Documentation: pkg.go.dev

# README

Config

The objective of the config package is to allow a client to tag a struct with (optional)default values and environment variables and then the config package will use viper to populate a struct.

Tag descriptions

TagMandatoryDescription
envYThe environment variable the field value will be retrieved from if the environment variable is present. N.B. It takes priority over any other tag.
fileNThe path to the file to take the default value from. This can be useful for things like passwords where they can be taken from docker / k8s secrets. N.B. If this tag is present and the file can be successfully processed then the default is redundant - if the value can not be read from the file and the default is present then the default is used.
defaultNThe default value which will be used if no environment variable is present. N.B. If this is not populated then the default for the type will be used i.e. 0 for int, "" for string, false for bool etc etc.

N.B. A nested struct does not need tags associated with it. Tags are only required for non struct entries.
N.N.B. Nested structs will use the "squash" property by default. That means no mapstructure tag is required.

Struct API

LoadViperConfig(cfg interface{}) error

Parameters

NameInput/OutputTypeDescription
cfginput/outputinterface{}A pointer to the config structure which will be populated on return.
-outputerror.This will hold an error if there are any issues with the tagging or viper was unable to unmarshal

File API

LoadViperConfigFromFile(filename string, cfg interface{}) error

Parameters

NameInput/OutputTypeDescription
filenameinputstringThe filename of the config file - this could be yaml, json, xml, env or ini. (i.e. anything which Viper supports)
cfginput/outputinterface{}A pointer to the config structure which will be populated on return.

Reader API

LoadViperConfigFromReader(reader io.Reader, cfg interface{}, cfgType string) error

Parameters

NameInput/OutputTypeDescription
readerinputio.ReaderAn io.reader which can be attached to a file, a string, a byte buffer etc etc
cfginput/outputinterface{}A pointer to the config structure which will be populated on return.
cfgTypeinputstringThe type of config contained within the reader - N.B. This is the range of supported viper types and can be json, yaml/yml, hcl, props, env, toml, ini. See viper docs for the full set.
-outputerror.This will hold an error if there are any issues with the tagging or viper was unable to unmarshal

Examples

Simple Struct

type TestStruct struct {       
   TestVal string `env:"TEST_VAL" default:"abc"`   
   TestFileVal string `env:"TEST_FILE_VAL" file:"/tmp/test_file_val"`  
} 

Nested Structs

//InnerStruct is the nested struct 
type InnerStruct struct {    
TestNestInner string `env:"TEST_NEST_INNER" default:"inner"`  
}
   
//OuterStruct is the struct containing the nested struct 
type OuterStruct struct {    
TestNestOuter string `env:"TEST_NEST_OUTER" default:"outer"` InnerStruct  
}

N.B. This library only supports anonymous nested structs.

Handling the response

The struct passed in will be populated upon return if no error.

var testStruct TestStruct
err := config.LoadViperConfig(&testStruct) 
if err != nil {      
	//Error handling code 
} 

# Functions

LoadViperConfig populates the cfg structure passed in (it must be the address passed in).
LoadViperConfigFromFile populates the cfg structure passed in (it must be the address passed in).
LoadViperConfigFromReader will populate the cfg structure passed in (it must be the address passed in).