config

package module
v0.0.26 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2020 License: MIT Imports: 8 Imported by: 0

README

Config

Simple configuration module. By default, it will load a configuration from a file (optional, can be either JSON or YAML if provided), env vars, then flags. A special flag will be exposed for your app "-configFile" which lets the app declare where to find a JSON or YAML config file.

Usage

First, create a struct that is a composition of config.BaseConfiguration, along with your other configuration attributes:

type ExampleConfig struct {
	config.BaseConfiguration
	Text      string
}

Then, call config.Load on a pointer ref to an instance of the newly-created struct. Ensure any property in the instance is initialized to what you want the default value of that field to be. Any slice should be initialize to an empty slice for best results:

tc := ExampleConfig{
    Text: "defaultText",
}
cfg, err := config.Load(&tc)
if err != nil {
    log.Fatal(err)
}

tcResult := cfg.(*ExampleConfig)

In this example, it will load a config file from wherever the -configFile flag references. If it is not present, a config file will not be loaded. If loaded, any value that is non-blank in the file will overwrite the matching value from the configuration instance initially passed.

Then, environment variables will be applied to the configuration. For each property, the environment variable is the upper-case name of the property, where dots are replaced with underscores. So something like "OAuth.clientId" will be OAUTH_CLIENTID.

Finally, any flag (CLI argument) will be applied. Each property is the dot property, where the dot is replaced by a dash. So, something like "database.url" would be database-url as a flag.

Using a Default Config File Path

To provide a default config file (to not require a flag/cli arg), you can initialize your config instance with BaseConfiguration.ConfigFile:

tc := ExampleConfig{
    BaseConfiguration: config.BaseConfiguration{
        ConfigFile: "./config.json",
    },
    Text: "",
}
Environment Variable Prefixes

For environment variables, this module also supports an environment variable prefix. To use this feature, provide a second argument for config.Load (the uppercase value of what is provided will be used, with a trailing underscore):

cfg, err := config.Load(&tc, "myapp")

This will make something like "MYAPP_TEXT" correspond to the "Text" property in the examples above. Usage of this feature is recommended so as not to collide with system environment variables as easily.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(configWithDefaultValues interface{}, envPrefix ...string) (interface{}, error)

Load will apply changes to the configWithDefaultValues object provided by the user. The precedence of value application (from most to least) is: flags, environment variables, file, then default configuration (provided by the user as the first parameter to this function). An optional "envPrefix" can be provided to this function to help differentiate its environment variable keys from those of the rest of the apps on the system.

Types

type BaseConfiguration

type BaseConfiguration struct {
	ConfigFile string `json:"configFile"`
}

BaseConfiguration is to be composed into your custom configuration model so this module can pick up the "configFile" flag to tell us which config file should be loaded

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL