Categorygithub.com/koding/multiconfig
modulepackage
0.0.0-20171124222453-69c27309b2d7
Repository: https://github.com/koding/multiconfig.git
Documentation: pkg.go.dev

# README

Multiconfig GoDoc Build Status

Load configuration from multiple sources. Multiconfig makes loading/parsing from different configuration sources an easy task. The problem with any app is that with time there are many options how to populate a set of configs. Multiconfig makes it easy by dynamically creating all necessary options. Checkout the example below to see it in action.

Features

Multiconfig is able to read configuration automatically based on the given struct's field names from the following sources:

  • Struct tags
  • TOML file
  • JSON file
  • YAML file
  • Environment variables
  • Flags

Install

go get github.com/koding/multiconfig

Usage and Examples

Lets define and struct that defines our configuration

type Server struct {
	Name    string `required:"true"`
	Port    int    `default:"6060"`
	Enabled bool
	Users   []string
}

Load the configuration into multiconfig:

// Create a new DefaultLoader without or with an initial config file
m := multiconfig.New()
m := multiconfig.NewWithPath("config.toml") // supports TOML, JSON and YAML

// Get an empty struct for your configuration
serverConf := new(Server)

// Populated the serverConf struct
err := m.Load(serverConf) // Check for error
m.MustLoad(serverConf)    // Panic's if there is any error

// Access now populated fields
serverConf.Port // by default 6060
serverConf.Name // "koding"

Run your app:

# Sets default values first which are defined in each field tag value. 
# Starts to load from config.toml
$ app

# Override any config easily with environment variables, environment variables
# are automatically generated in the form of STRUCTNAME_FIELDNAME
$ SERVER_PORT=4000 SERVER_NAME="koding" app

# Or pass via flag. Flags are also automatically generated based on the field
# name
$ app -port 4000 -users "gopher,koding"

# Print dynamically generated flags and environment variables:
$ app -help
Usage of app:
  -enabled=true: Change value of Enabled.
  -name=Koding: Change value of Name.
  -port=6060: Change value of Port.
  -users=[ankara istanbul]: Change value of Users.

Generated environment variables:
   SERVER_NAME
   SERVER_PORT
   SERVER_ENABLED
   SERVER_USERS

License

The MIT License (MIT) - see LICENSE for more details

# Functions

MultiLoader creates a loader that executes the loaders one by one in order and returns on the first error.
MultiValidator accepts variadic validators and satisfies Validator interface.
MustLoad loads with the DefaultLoader settings.
MustLoadWithPath loads with the DefaultLoader settings and from the given Path.
New returns a new instance of DefaultLoader without any file loaders.
NewWithPath returns a new instance of Loader to read from the given configuration file.

# Variables

ErrFileNotFound states that given file is not exists.
ErrSourceNotSet states that neither the path or the reader is set on the loader.

# Structs

DefaultLoader implements the Loader interface.
EnvironmentLoader satisifies the loader interface.
FlagLoader satisfies the loader interface.
JSONLoader satisifies the loader interface.
RequiredValidator validates the struct against zero values.
TagLoader satisfies the loader interface.
TOMLLoader satisifies the loader interface.
YAMLLoader satisifies the loader interface.

# Interfaces

Loader loads the configuration from a source.
Validator validates the config against any predefined rules, those predefined rules should be given to this package.