conf

package module
v0.0.0-...-869bd38 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2013 License: MIT Imports: 4 Imported by: 3

README

Conf

A configuration loader in Go. Load configuration with files, environment variables and command-line arguments.

Load Configuration from Multiple Sources

Configuration management can get complicated very quickly for even trivial applications running in production. conf addresses this problem by enabling you to load configuration from different sources. The order in which you attach these configuration sources determines their priority. The last attached source has the highest priority. These are the supported sources:

Here is an example of loading configuration in priority:

package main

import "github.com/jingweno/conf"

func main() {
  loader := conf.NewLoader()

  // 1. any overrides
  loader.Overrides(
    map[string]interface{}{
      "always": "be this value",
    }
  )

  // 2. environment variables
  // 3. command line arguments
  loader.Env().Argv()

  // 4. values from `config.json`
  loader.File("/path/to/config.json")

  // 5. more values from `another_config.json`
  loader.File("/path/to/another_config.json")

  // 6. any default values
  loader.Defaults(
    map[string]interface{}{
      "if nothing else": "use this value"
    }
  )

  c, err := loader.Load()
  // do something with c
}

Example

Consider the following Go code:

package main

import (
	"fmt"
	"github.com/jingweno/conf"
	"os"
)

func main() {
	d := map[string]interface{}{
		"GO_ENV":        "development",
		"DATABASE_NAME": "example_development",
		"DATABASE_POOL": 5,
	}
	c, err := conf.NewLoader().Argv().Env().File("./config.json").Defaults(d).Load()

	if err != nil {
		fmt.Fprintf(os.Stderr, "err: %s\n", err)
		return
	}

	printConf(c, "GO_ENV")
	printConf(c, "DATABASE")
	printConf(c, "DATABASE_NAME")
	printConf(c, "DATABASE_HOST")
	printConf(c, "DATABASE_PORT")
	printConf(c, "DATABASE_POOL")
}

func printConf(c *conf.Conf, k string) {
	fmt.Printf("%s: %v\n", k, c.Get(k))
}

and a config.json:

{
  "DATABASE": "postgres",
  "DATABASE_HOST": "127.0.0.1",
  "DATABASE_PORT": "1234"
}

If you run the above code:

$ GO_ENV=production go run example.go --DATABASE_POOL 10

The output will be:

GO_ENV: production
DATABASE: postgres
DATABASE_NAME: example_development
DATABASE_HOST: 127.0.0.1
DATABASE_PORT: 1234
DATABASE_POOL: 10

More examples are available.

License

conf is released under the MIT license. See LICENSE.md.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter interface {
	Apply(conf *Conf) error
}

type Conf

type Conf struct {
	// contains filtered or unexported fields
}

func (*Conf) Bool

func (c *Conf) Bool(key string) bool

func (*Conf) Get

func (c *Conf) Get(key string) interface{}

func (*Conf) Int

func (c *Conf) Int(key string) int

func (*Conf) Merge

func (c *Conf) Merge(m map[string]interface{})

func (*Conf) Set

func (c *Conf) Set(key string, val interface{}) interface{}

func (*Conf) Size

func (c *Conf) Size() int

func (*Conf) String

func (c *Conf) String(key string) string

type Loader

type Loader struct {
	// contains filtered or unexported fields
}

func NewLoader

func NewLoader() *Loader

Create a Loader

func (*Loader) Argv

func (l *Loader) Argv() *Loader

Load configuration from command line arguments

func (*Loader) Defaults

func (l *Loader) Defaults(m map[string]interface{}) *Loader

Define default configuration

func (*Loader) Env

func (l *Loader) Env() *Loader

Load configuration from environment variables

func (*Loader) File

func (l *Loader) File(path string) *Loader

Load configuration from a JSON file

func (*Loader) Load

func (l *Loader) Load() (*Conf, error)

Load configuration with registered Adapters

func (*Loader) Overrides

func (l *Loader) Overrides(m map[string]interface{}) *Loader

Override configuration loaded from any Adapter

func (*Loader) Register

func (l *Loader) Register(a Adapter) *Loader

Register a custom Adapter for loading configuration

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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