Code Monkey home page Code Monkey logo

conf's Introduction

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.

conf's People

Contributors

owenthereal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

pengux vsrc

conf's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.