Back to godoc.org
github.com/ian-kent/gofigure

package gofigure

v0.0.0 (c9dc3a1)
Latest Go to latest
Published: May 2, 2017 | License: MIT | Module: github.com/ian-kent/gofigure

Overview

Package gofigure simplifies configuration of Go applications.

Define a struct and call Gofigure()

Index

Examples

Variables

var Debug = false

Debug controls log output

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

DefaultOrder sets the default order used

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

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

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

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

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

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

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

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

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

Code:

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)
Example (WithDefault)

Code:

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)
Example (WithNestedStruct)

Code:

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)
Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier