Go configuration made easy!

gofigure GoDoc Build Status Coverage Status

Go configuration made easy!

  • Just define a struct and call Gofigure
  • Supports strings, ints/uints/floats, slices and nested structs
  • Supports environment variables and command line flags

Requires Go 1.2+ because of differences in Go's flag package.

Example

go get github.com/ian-kent/gofigure

package main

import "github.com/ian-kent/gofigure"

type config struct {
  gofigure interface{} `envPrefix:"BAR" order:"flag,env"`
  RemoteAddr string `env:"REMOTE_ADDR" flag:"remote-addr" flagDesc:"Remote address"`
  LocalAddr  string `env:"LOCAL_ADDR" flag:"local-addr" flagDesc:"Local address"`
  NumCPU int `env:"NUM_CPU" flag:"num-cpu" flagDesc:"Number of CPUs"`
  Sources []string `env:"SOURCES" flag:"source" flagDesc:"Source URL (can be provided multiple times)"`
  Numbers []int `env:"NUMBERS" flag:"number" flagDesc:"Number (can be provided multiple times)"`
  Advanced struct{
      MaxBytes int64 `env:"MAX_BYTES" flag:"max-bytes" flagDesc:"Max bytes"`
      MaxErrors int64  `env:"MAX_ERRORS" flag:"max-errors" flagDesc:"Max errors"`
  }
}

func main() {
  var cfg config
  err := gofigure.Gofigure(&cfg)
  if err != nil {
    log.Fatal(err)
  }
  // use cfg
}

gofigure field

The gofigure field is used to configure Gofigure.

The order tag is used to set configuration source order, e.g. environment variables first then command line options second.

Any field matching camelCase format will be parsed into camel and case, and passed to the source matching camel.

For example, the envPrefix field is split into env and prefix, and the tag value is passed to the environment variable source as the prefix parameter.

Arrays and environment variables

Array support for environment variables is currently experimental.

To enable it, set GOFIGURE_ENV_ARRAY=1.

When enabled, the environment variable is split on commas, e.g.

struct {
    EnvArray []string `env:"MY_ENV_VAR"`
}

MY_ENV_VAR=a,b,c

EnvArray = []string{"a", "b", "c"}

Licence

Copyright ©‎ 2014, Ian Kent (http://www.iankent.eu).

Released under MIT license, see LICENSE for details.

Owner
Ian Kent
Love code... mostly Go, now a bit of Elm. Created @mailhog and @websysd
Ian Kent
Comments
  • Cannot get application using gofigure to work with tests/goconvey

    Cannot get application using gofigure to work with tests/goconvey

    Have added test.v and test.timeout to application struct{} to get tests working, but is a bit ugly. However test parameter -convey-json is causing grief as it is supplied by goconvey without a parameter, and gofigure expects a parameter, even if the flag is bool. golang flag does not expect a parameter.

    Not resolved the problem yet. Any help would be appreciated.

    It might be nice to automatically accept the known test parameters in gofigure, if running in test mode (or not complain if a command line flag is given that is not declared).

  • Feature request: Expose access to `SetOutput` and maybe `Usage`

    Feature request: Expose access to `SetOutput` and maybe `Usage`

    It would be useful to be able to access Usage and SetOutput in particular, so any logs/usage dumpage can get emitted by structured logging instead of the default Stdout/err.

    I thought that the following would work in the app:

     // Capture flag output 
     buf := new(bytes.Buffer)
     flag.CommandLine.SetOutput(buf)
    
     err := gofigure.Gofigure(cfg)
    
     // We may have output to spew:
     if buf.Len() > 0 {
         log.Error(fmt.Errorf(buf.String()))
     }
    

    but alas.... no :-(

    Either the execution order is not what I expect, or flag.CommandLine is being reset (though I thought I had 'cured' that (see issue on "test flags not working"). Maybe something else!

  • Debug in registerFields() incorrect ouput

    Debug in registerFields() incorrect ouput

    Debug output in registerFields() outputs the wrong values, such as:

    2017/03/21 13:51:03 Registering 'ConveyJson' for source 'env' with key 'ConveyJson'
    2017/03/21 13:51:03 Registering 'ConveyJson' for source 'flag' with key 'CONVEY_JSON'
    

    This should be

    2017/03/21 13:51:03 Registering 'ConveyJson' for source 'env' with key 'CONVEY_JSON'
    2017/03/21 13:51:03 Registering 'ConveyJson' for source 'flag' with key 'convey-json'
    

    Debug printf is in the wrong place. Have fixed on a branch and will issue pull request.

  • Detection of GOFIGURE_DEBUG failes

    Detection of GOFIGURE_DEBUG failes

    https://github.com/ian-kent/gofigure/blob/master/gofigure.go#L21

    Have fixed as below and will issue pull request at some point!

    var _ = func() (_ struct{}) {
       ...
    }()
    
  • Nondeterministic flags when using nested structs

    Nondeterministic flags when using nested structs

    Earlier we noticed an issue with gofigure when using nested structs in our configuration struct. Each time we run our Go binary, the command line flags which are accepted by the application were constantly changing.

    This can be reproduced by doing the following.

    • Define a config struct with nested structs
    • Run your Go binary multiple times with the -h flag

    You should then see that the usage output changes each time.

  • Add gofigure:

    Add gofigure:"ignore" support

    A simple change that allows a field of a struct to be ignored if it is tagged with gofigure:"ignore" This can be useful for unsupported field types or configuration that is rather derived from other parameters, not read directly.

Configure is a Go package that gives you easy configuration of your project through redundancy

Configure Configure is a Go package that gives you easy configuration of your project through redundancy. It has an API inspired by negroni and the fl

Sep 26, 2022
A simple multi-layered config loader for Go. Made for smaller projects. No external dependencies.

config ⚠️ Work in progress! A simple multi-layered config loader for Go. Made for smaller projects. No external dependencies. Installation go get -u g

Dec 26, 2021
✨Clean and minimalistic environment configuration reader for Golang

Clean Env Minimalistic configuration reader Overview This is a simple configuration reading tool. It just does the following: reads and parses configu

Jan 8, 2023
12 factor configuration as a typesafe struct in as little as two function calls

Config Manage your application config as a typesafe struct in as little as two function calls. type MyConfig struct { DatabaseUrl string `config:"DAT

Dec 13, 2022
JSON or YAML configuration wrapper with convenient access methods.

Config Package config provides convenient access methods to configuration stored as JSON or YAML. This is a fork of the original version. This version

Dec 16, 2022
An opinionated configuration loading framework for Containerized and Cloud-Native applications.
An opinionated configuration loading framework for Containerized and Cloud-Native applications.

Opinionated configuration loading framework for Containerized and 12-Factor compliant applications. Read configurations from Environment Variables, an

Dec 16, 2022
Load configuration in cascade from multiple backends into a struct
Load configuration in cascade from multiple backends into a struct

Confita is a library that loads configuration from multiple backends and stores it in a struct. Supported backends Environment variables JSON files Ya

Jan 1, 2023
Small library to read your configuration from environment variables

envconfig envconfig is a library which allows you to parse your configuration from environment variables and fill an arbitrary struct. See the example

Nov 3, 2022
A minimalist Go configuration library
A minimalist Go configuration library

fig fig is a tiny library for loading an application's config file and its environment into a Go struct. Individual fields can have default values def

Dec 23, 2022
go-up! A simple configuration library with recursive placeholders resolution and no magic.

go-up! A simple configuration library with placeholders resolution and no magic. go-up provides a simple way to configure an application from multiple

Nov 23, 2022
goconfig uses a struct as input and populates the fields of this struct with parameters from command line, environment variables and configuration file.

goconfig goconfig uses a struct as input and populates the fields of this struct with parameters from command line, environment variables and configur

Dec 15, 2022
Harvest configuration, watch and notify subscriber

Harvester Harvester is a configuration library which helps setting up and monitoring configuration values in order to dynamically reconfigure your app

Dec 26, 2022
go implementation of lightbend's HOCON configuration library https://github.com/lightbend/config

HOCON (Human-Optimized Config Object Notation) Configuration library for working with the Lightbend's HOCON format. HOCON is a human-friendly JSON sup

Dec 3, 2022
🛠 A configuration library for Go that parses environment variables, JSON files, and reloads automatically on SIGHUP
🛠 A configuration library for Go that parses environment variables, JSON files, and reloads automatically on SIGHUP

config A small configuration library for Go that parses environment variables, JSON files, and reloads automatically on SIGHUP. Example func main() {

Dec 11, 2022
Golang library for managing configuration data from environment variables

envconfig import "github.com/kelseyhightower/envconfig" Documentation See godoc Usage Set some environment variables: export MYAPP_DEBUG=false export

Dec 26, 2022
Light weight, extensible configuration management library for Go. Built in support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.
Light weight, extensible configuration management library for Go. Built in support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.

koanf (pronounced conf; a play on the Japanese Koan) is a library for reading configuration from different sources in different formats in Go applicat

Jan 8, 2023
A golang package for parsing ini-style configuration files

Mini Mini is a simple ini configuration file parser. The ini syntax supported includes: The standard name=value Comments on new lines starting with #

Jan 7, 2023
A dead simple configuration manager for Go applications

Store Store is a dead simple configuration manager for Go applications. I didn't like existing configuration management solutions like globalconf, tac

Dec 24, 2022
Go configuration with fangs
Go configuration with fangs

Viper v2 feedback Viper is heading towards v2 and we would love to hear what you would like to see in it. Share your thoughts here: https://forms.gle/

Jan 8, 2023