Documentation ¶
Overview ¶
Built-in mechanism for loading configurations(properties) from various source.
ConfigLoader ¶
You can use "DefaultLoader" to load configuration by default.
evn := DefaultLoader.New(). ParseFlags(). Load()
Documentation: https://github.com/mikelue/go-misc/blob/master/ioc/frangipani/README.md
ConfigBuilder ¶
You can use this object to customized the behavior of loading configurations.
env := NewConfigBuilder(). Priority(CL_ENVVAR, CL_ARGS, CL_CONFIG_FILE, CL_PWD). Prefix("dog"). Build(). ParseFlags().Load()
Index ¶
- Constants
- type ConfigBuilder
- func (self *ConfigBuilder) Build() ConfigLoader
- func (self *ConfigBuilder) DefaultWithMap(properties map[string]interface{}) *ConfigBuilder
- func (self *ConfigBuilder) DefaultWithViper(viper *viper.Viper) *ConfigBuilder
- func (self *ConfigBuilder) Pflags(newFlags *pflag.FlagSet) *ConfigBuilder
- func (self *ConfigBuilder) Prefix(prefix string) *ConfigBuilder
- func (self *ConfigBuilder) Priority(sources ...ConfigSource) *ConfigBuilder
- type ConfigLoader
- type ConfigSource
- type IDefaultLoader
Examples ¶
Constants ¶
const ( // Properties packed as JSON format FLAG_CONFIG_JSON = ".config.json" // Properties packed as YAML format FLAG_CONFIG_YAML = ".config.yaml" // Properties from external file FLAG_CONFIG_FILES = PROP_CONFIG_FILES // For activated profiles(see frangipani) FLAG_ACITVE_PROFILES = PROP_PROFILES_ACTIVE )
const ( // Name of environment variable for packed properties as JSON format ENVVAR_JSON = "_CONFIG_JSON" // Name of environment variable for packed properties as YAML format ENVVAR_YAML = "_CONFIG_YAML" // Name of environment variable for external file ENVVAR_FILE = "_CONFIG_FILES" // Name of environment variable for activated profiles(see frangipani) ENVVAR_PROFILES_ACTIVE = "_PROFILES_ACTIVE" )
const ( // Default prefix of loading mechanism DEFAULT_PREFIX = "fgapp" // Default name of logger // // See: https://github.com/go-eden/slf4go-logrus LOGGER_NAME_CONFIG = "fgapp.config" // The configuration file PROP_CONFIG_FILES = ".config.files" // The profiles to be activated PROP_PROFILES_ACTIVE = ".profiles.active" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConfigBuilder ¶
type ConfigBuilder struct {
// contains filtered or unexported fields
}
This object is the center of customizing loading mechanisms of configurations.
Example ¶
oldOsArgs := os.Args defer func() { os.Args = oldOsArgs }() // This is just to make testing re-runnable pflag.CommandLine = pflag.NewFlagSet("ExampleIDefaultLoader_New", pflag.ExitOnError) os.Args = []string{ `--guava.config.yaml={ db.host: apple-linux, db.port: 8871 }`, } os.Setenv( "GUAVA_CONFIG_YAML", `{ db.host: guava-linux }`, ) // Makes the source of environment variables having higher priority than arguments typedProperties := NewConfigBuilder(). Prefix("guava"). Priority(CL_ENVVAR, CL_ARGS). Build(). ParseFlags().Load(). Typed() fmt.Printf("%s:%d", typedProperties.GetString("db.host"), typedProperties.GetInt("db.port"))
Output: guava-linux:8871
func NewConfigBuilder ¶
func NewConfigBuilder() *ConfigBuilder
Consturcts a new builder so that you can custoimze the loading of configurations.
func (*ConfigBuilder) Build ¶
func (self *ConfigBuilder) Build() ConfigLoader
Builds the "ConfigLoader", which is used to load an instance of "Environment".
func (*ConfigBuilder) DefaultWithMap ¶
func (self *ConfigBuilder) DefaultWithMap(properties map[string]interface{}) *ConfigBuilder
Sets up the default properties, this has the lowest priority set by "Priority".
func (*ConfigBuilder) DefaultWithViper ¶
func (self *ConfigBuilder) DefaultWithViper(viper *viper.Viper) *ConfigBuilder
Sets up the default properties(by viper), this has the lowest priority set by "Priority".
See spf13/Viper: https://github.com/spf13/viper
func (*ConfigBuilder) Pflags ¶
func (self *ConfigBuilder) Pflags(newFlags *pflag.FlagSet) *ConfigBuilder
Sets-up customized flags for parsing of arguments
func (*ConfigBuilder) Prefix ¶
func (self *ConfigBuilder) Prefix(prefix string) *ConfigBuilder
Sets the prefix of loading.
This can affect naming of various sources.
See detail: https://github.com/mikelue/go-misc/blob/master/ioc/frangipani/README.md
func (*ConfigBuilder) Priority ¶
func (self *ConfigBuilder) Priority(sources ...ConfigSource) *ConfigBuilder
Sets the priority of supported sources.
The higher priority of sources should be put in front of others.
See: ConfigSource
type ConfigLoader ¶
type ConfigLoader interface { // Loads the environment object Load() fg.Environment // Parse the flags ParseFlags() ConfigLoader }
To load a bunch of sources as "Environment".
type ConfigSource ¶
type ConfigSource int
CL - stands for [C]onfiguration [L]oading
const ( // The source comes from default names of files in $XDG_CONFIG_HOME CL_XDG ConfigSource = 1 // The source comes from arguments: // --fgapp.config.yaml // --fgapp.config.json // --fgapp.config.config.files // --fgapp.config.files // --fgapp.profiles.active CL_ARGS ConfigSource = 2 // The source comes from environment variables: // $FGAPP_CONFIG_YAML // $FGAPP_CONFIG_JSON // $FGAPP_CONFIG_FILES // $FGAPP_PROFILES_ACTIVE CL_ENVVAR ConfigSource = 3 // The source comes from provided file name CL_CONFIG_FILE ConfigSource = 4 // The source comes default names of files in working directory // // See: os.Getwd() CL_PWD ConfigSource = 5 // The source comes default names of files in directory of command // // See: os.Args[0] CL_CMDDIR ConfigSource = 6 )
type IDefaultLoader ¶
type IDefaultLoader int
var DefaultLoader IDefaultLoader
Method space used to construct new instance of "ConfigLoader"
func (*IDefaultLoader) New ¶
func (self *IDefaultLoader) New() ConfigLoader
With default setting for loading configurations
Example ¶
// This is just to make testing re-runnable pflag.CommandLine = pflag.NewFlagSet("ExampleIDefaultLoader_New", pflag.ExitOnError) os.Setenv( "FGAPP_CONFIG_YAML", `{ db.host: dev-linux, db.port: 1980 }`, ) typedProperties := DefaultLoader.New(). ParseFlags().Load(). Typed() fmt.Printf("%s:%d", typedProperties.GetString("db.host"), typedProperties.GetInt("db.port"), )
Output: dev-linux:1980
func (*IDefaultLoader) WithMap ¶
func (*IDefaultLoader) WithMap(properties map[string]interface{}) ConfigLoader
With some default values(as map) and settings for loading configurations
Example ¶
// This is just to make testing re-runnable pflag.CommandLine = pflag.NewFlagSet("ExampleIDefaultLoader_New", pflag.ExitOnError) os.Setenv( "FGAPP_CONFIG_YAML", `{ db.host: dev-linux }`, ) typedProperties := DefaultLoader. WithMap(map[string]interface{}{ "db.port": 87, }). ParseFlags().Load(). Typed() fmt.Printf("%s:%d", typedProperties.GetString("db.host"), typedProperties.GetInt("db.port"), )
Output: dev-linux:87
func (*IDefaultLoader) WithViper ¶
func (*IDefaultLoader) WithViper(viper *viper.Viper) ConfigLoader
With some default values(as "*viper.Viper") and settings for loading configurations
Example ¶
// This is just to make testing re-runnable pflag.CommandLine = pflag.NewFlagSet("ExampleIDefaultLoader_New", pflag.ExitOnError) os.Setenv( "FGAPP_CONFIG_YAML", `{ db.host: dev-linux }`, ) viper := viper.New() viper.Set("db.port", 998) typedProperties := DefaultLoader. WithViper(viper). ParseFlags().Load(). Typed() fmt.Printf("%s:%d", typedProperties.GetString("db.host"), typedProperties.GetInt("db.port"), )
Output: dev-linux:998