Documentation ¶
Overview ¶
Package config provides an easy way to create and manage configurations for aergo projects written in go.
By extending 'IContext' interface, developers can easily use a porject-specific configurations. It requires to provide base information.
type IContext interface { GetDefaultConfig() interface{} //a default config struct filled with default values GetHomePath() string // a default home path GetConfigFileName() string // a config file name GetTemplate() string // a default toml config file template. }
Internally, the pkg has functions to find and manage a configuration file, and automatically generates toml file using the template, if the file does not exist. The template is filled with default values of GetDefaultConfig() The template must follow this format
string_type = "{{.STRUCT.FILED}}" bool/number_type = {{.STRUCT.FILED}} string_array_type = [{{range .STRUCT.FILED}} "{{.}}", {{end}} ] bool/number_array_type = [{{range .STRUCT.FILED}} {{.}}, {{end}} ] map = does not support [substruct] field_in_substruct = ... ...
.STRUCT and .FIELD must have a same name with a matched exported struct or field. You can find a detail example at a 'config_test.go' file.
If there exists multiple configration files, a priorty is
- filename given using a flag
- home set at env
- a default os home dir'
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseContext ¶
BaseContext has IContext interface to get default information to find or generate a configuration file, and viper instance to store and perform actions
func NewBaseContext ¶
func NewBaseContext(contextImpl IContext, homePath string, configFilePath string, envPrefix string) BaseContext
NewBaseContext returns a BaseContext that is used to load configuration easily.
This functions requests an IContext that contains basic config setting vars.
Other parameters(homePath, configFilePath, envPrefix) are optional. If you do not insert them, then this will automatically infer home and config path using a context of the given contextImpl. (see retrievePath func) If you insert only homePath, then this will try to find a config file at that path. Or if you insert only configFilePath, then this will try to open that file.
This only supports a toml extension.
func (*BaseContext) BindPFlags ¶
func (ctx *BaseContext) BindPFlags(flagSet *pflag.FlagSet)
BindPFlags binds a flagSet, given at a command line, to this context and maps to configuration parameters
func (*BaseContext) ExpandPathEnv ¶
func (ctx *BaseContext) ExpandPathEnv(path string) string
ExpandPathEnv is similar with 'os.ExpandEnv(s string)' func. This replace ${var} of $var in a path string according to the values of the current environment in the context. Undefined values are replaced by the empty string.
func (*BaseContext) LoadOrCreateConfig ¶
func (ctx *BaseContext) LoadOrCreateConfig(defaultConf interface{}) error
LoadOrCreateConfig loads a config file using the information in a context. If the config file or it's parent directory doesn't exist, then this creats those first using a default name and an automatically generated configration text.