Giter Club home page Giter Club logo

ezconf's Introduction

EZConf Build Status codecov Go Report Card

Go library to provide simple way of reading configuration settings from four sources, with each source able to override the previous:

  1. The default settings for your app
  2. A TOML file with settings
  3. Environment variables mapping to your top level settings
  4. Command line parameters mapping to your top level settings

To use it, you only need to create a struct representing the desired configuration and create an instance with the defaults for your app. You can then pass that struct to the library and read the settings from all the sources above.

The library will automatically parse command line parameters and environment variables for all top level fields in your struct of the following types:

  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, uint64
  • float32, float64
  • bool
  • string
  • time.Time as strings in the following formats: 2018-04-02, 15:30:02, 2018-04-02T15:30:02.000 and 2018-04-03T05:30:00.123+07:00
  • slog.Level as strings e.g. info

It converts all CamelCase fields to snake_case in a manner that is compatible with the acronyms we work with everyday. Some examples of how a struct name is converted to a TOML field, environment variable and command line parameter can be found below.

Environment variables are prefixed with your app name, in this case courier:

Struct Field TOML Field Environment Variable Command line Parameter
AWSRegion aws_region COURIER_AWS_REGION aws-region
EC2InstanceID ec2_instance_id COURIER_EC2_INSTANCE_ID ec2-instance-id
DB db COURIER_DB db
NumWorkers num_workers COURIER_NUM_WORKERS num-workers

EZConf will also automatically create the appropriate flags and help based on your struct definition, for example:

% courier -help
Courier - a fast message broker for IP and SMS messages

Usage of courier:
  -aws-region string
    	the aws region that S3 buckets are in (default "us-west-2")
  -db string
    	the url describing how to connect to the database (default "postgres://user@secret:rds-internal.foo.bar/courier")
  -debug-conf
    	print where config values are coming from
  -ec2-instance-id string
    	the id of the ec2 instance we are running on (default "i-12355111134a")
  -help
    	print usage information
  -num-workers int
    	the number of workers to start (default 32)

Environment variables:
          COURIER_AWS_REGION - string
                  COURIER_DB - string
     COURIER_EC2_INSTANCE_ID - string
         COURIER_NUM_WORKERS - int

Example

package main

import (
	"fmt"

	"github.com/nyaruka/ezconf"
)

// Config is our apps configuration struct, we can use the `help` tag to add usage information
type Config struct {
	AWSRegion     string `help:"the aws region that S3 buckets are in"`
	DB            string `help:"the url describing how to connect to the database"`
	EC2InstanceID string `help:"the id of the ec2 instance we are running on"`
	NumWorkers    int    `help:"the number of workers to start"`
}

func main() {
	// instantiate our config with our defaults
	config := &Config{
		AWSRegion:     "us-west-2",
		DB:            "postgres://user@secret:rds-internal.foo.bar/courier",
		EC2InstanceID: "i-12355111134a",
		NumWorkers:    32,
	}

	// create our loader object, configured with configuration struct (must be a pointer), our name
	// and description, as well as any files we want to search for
	loader := ezconf.NewLoader(
		config,
		"courier", "Courier - a fast message broker for IP and SMS messages",
		[]string{"courier.toml"},
	)

	// load our configuration, exiting if we encounter any errors
	loader.MustLoad()

	// our settings have now been loaded into our config struct
	fmt.Printf("Final Settings:\n%+v\n", *config)

	// if we wish we can also validate our config using our favorite validation library
}

ezconf's People

Contributors

nicpottier avatar rowanseymour avatar

Stargazers

 avatar Zhanliang Liu avatar ik5 avatar taketori at x10d.jp avatar James Pereira avatar Lukas Malkmus avatar Alexey Agafonov avatar Everton Yoshitani avatar

Watchers

 avatar Eric Newcomer avatar Everton Yoshitani avatar  avatar James Cloos avatar  avatar

ezconf'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.