gofigure

package module
v0.0.0-...-c9dc3a1 Latest Latest
Warning

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

Go to latest
Published: May 2, 2017 License: MIT Imports: 9 Imported by: 16

README

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.

Documentation

Overview

Package gofigure simplifies configuration of Go applications.

Define a struct and call Gofigure()

Index

Examples

Constants

This section is empty.

Variables

View Source
var Debug = false

Debug controls log output

View Source
var DefaultOrder = []string{"env", "flag"}

DefaultOrder sets the default order used

View Source
var ErrInvalidOrder = errors.New("Invalid order")

ErrInvalidOrder is returned if the "order" struct tag is invalid

View Source
var ErrUnsupportedFieldType = errors.New("Unsupported field type")

ErrUnsupportedFieldType is returned for unsupported field types, e.g. chan or func

View Source
var ErrUnsupportedType = errors.New("Unsupported interface type")

ErrUnsupportedType is returned if the interface isn't a pointer to a struct

View Source
var Sources = map[string]sources.Source{
	"env":  &sources.Environment{},
	"flag": &sources.CommandLine{},
}

Sources contains a map of struct field tag names to source implementation

Functions

func Gofigure

func Gofigure(s interface{}) error

Gofigure parses and applies the configuration defined by the struct.

It returns ErrUnsupportedType if s is not a pointer to a struct.

Example
os.Args = []string{"gofigure", "-remote-addr", "localhost:8080"}
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)

type example 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)"`
}

var cfg example

// Pass a reference to Gofigure
err := Gofigure(&cfg)
if err != nil {
	log.Fatal(err)
}

// Fields on cfg should be set!
fmt.Printf("%+v", cfg)
Output:

Example (WithDefault)
os.Args = []string{"gofigure", "-remote-addr", "localhost:8080"}
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)

type example 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)"`
}

var cfg = example{
	RemoteAddr: "localhost:6060",
	LocalAddr:  "localhost:49808",
	NumCPU:     10,
	Sources:    []string{"test1.local", "test2.local"},
	Numbers:    []int{1, 2, 3},
}

// Pass a reference to Gofigure
err := Gofigure(&cfg)
if err != nil {
	log.Fatal(err)
}

// Fields on cfg should be set!
fmt.Printf("%+v", cfg)
Output:

Example (WithNestedStruct)
os.Args = []string{"gofigure", "-remote-addr", "localhost:8080", "-local-addr", "localhost:49808"}
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)

type example struct {
	gofigure   interface{} `envPrefix:"BAR" order:"flag,env"`
	RemoteAddr string      `env:"REMOTE_ADDR" flag:"remote-addr" flagDesc:"Remote address"`
	Advanced   struct {
		LocalAddr string `env:"LOCAL_ADDR" flag:"local-addr" flagDesc:"Local address"`
	}
}

var cfg example

// Pass a reference to Gofigure
err := Gofigure(&cfg)
if err != nil {
	log.Fatal(err)
}

// Fields on cfg should be set!
fmt.Printf("%+v", cfg)
Output:

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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