Giter Site home page Giter Site logo

confer's Introduction

Confer

Build Status

A viper-derived configuration management package.

Significant changes include:

  • Materialized path access of configuration variables.
  • The singleton has been replaced by separate instances, largely for testability.
  • The ability to load and merge multiple configuration files.

Features

  1. Merging multiple configuration sources.
  config.ReadPaths("application.yaml", "environments/production.yaml")`
  1. Materialized path access of nested configuration data.
  config.GetInt('app.database.port')
  1. Binding of environment variables to configuration data.

    APP_DATABASE_PORT=3456 go run app.go

  2. User-defined helper methods.

Usage

Initialization

Create your configuration instance:

config := confer.NewConfig()

Then set defaults, read paths, set overrides:

config.SetDefault("environment", "development")
config.ReadPaths("application.yaml", "environments/production.yml")
config.Set("environment", "development")

No worries! Confer will conveniently merge deeply nested structures for you. My usual configuration setup looks like this:

config
  ├── application.development.yml
  ├── application.production.yml
  └── application.yml

For example, an application-specific config package like the one below can be used to drive a core configuration with environment specific overrides:

var App *confer.Config

func init() {
  App = confer.NewConfig()
  appenv := os.Getenv("MYAPP_ENV");
  paths := []string{"application.yml"}

  if (appenv != "") {
    paths = append(paths, fmt.Sprintf("application.%s.yml", appenv))
  }

  if err := App.ReadPaths(paths...); err != nil {
    log.Warn(err)
  }
}

Setting Defaults

Sets a value if it hasn't already been set. Multiple invocations won't clobber existing values, so you'll likely want to do this before reading from files.

config := confer.NewConfig()
config.ReadPaths("application.yaml")
config.SetDefault("ContentDir", "content")
config.SetDefault("LayoutDir", "layouts")
config.SetDefault("Indexes", map[string]string{"tag": "tags", "category": "categories"})

Setting Keys / Value Pairs

Sets a value. Has lower precedence than environment variables or command line flags.

config.Set("verbose", true)
config.Set("logfile", "/var/log/config.log")

Getting Values

There are a variety of accessors for accessing type-coerced values:

Get(key string) : interface{}
GetBool(key string) : bool
GetFloat64(key string) : float64
GetInt(key string) : int
GetString(key string) : string
GetStringMap(key string) : map[string]interface{}
GetStringMapString(key string) : map[string]string
GetStringSlice(key string) : []string
GetTime(key string) : time.Time
IsSet(key string) : bool

Deep Configuration Data

Materialized paths allow easy access of deeply nested config data:

logger_config := config.GetStringMap("logger.stdout")

Because periods aren't valid environment variable characters, when using automatic environment bindings (see below), substitute with underscores:

LOGGER_STDOUT=/var/log/myapp go run server.go

Environment Bindings

Automatic Binding

Confer can automatically bind all existing configuration keys to environment variables.

Given some sort of application.yaml

---
app:
   log: "verbose"
   database:
       host: "localhost"

And this pair of calls:

config.ReadPaths("application.yaml")
config.AutomaticEnv()

You'll have the following environment variables exposed for configuration:

APP_LOG
APP_DATABASE_HOST
Selective Binding

If this automatic binding is bizarre, you can selectively bind environment variables with ``BindEnv()`.

config.BindEnv("APP_LOG", "app.log")

Helpers

You can Set a func() interface{} at a configuration key to provide values dynamically:

config.Set("dbstring", func() interface {} {
  return fmt.Sprintf(
    "user=%s dbname=%s sslmode=%s",
    config.GetString("database.user"),
    config.GetString("database.name"),
    config.GetString("database.sslmode"),
  )
})
assert(config.GetString("dbstring") ==  "user=doug dbname=pruden sslmode=pushups")

confer's People

Contributors

berkus avatar bketelsen avatar chrishamant avatar davidpelaez avatar derekdowling avatar fzerorubigd avatar giodamelio avatar jacobstr avatar markgustetic avatar natefinch avatar spf13 avatar stp-ip 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  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  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  avatar  avatar  avatar

Watchers

 avatar  avatar

confer's Issues

configReader should not call jww.Fatalf

In ./reader/configreader.go you call jww.Fatalf() which calls log.Fatalf() which calls os.Exit(1) which provides no opportunity for the parent program to recover from a configuration exception, even if done in a goroutine.

Recommend updating these jww.Fatalf() calls to jww.Panicf() so that the program can recover if they have a clean path to do so.

Happy to put together a pull request if you would like.

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.