gofigure: github.com/ian-kent/gofigure Index | Examples | Files | Directories

package gofigure

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

Package gofigure simplifies configuration of Go applications.

Define a struct and call Gofigure()

Index

Examples

Package Files

gofigure.go

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 Uses

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.

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)

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)

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)

Directories

PathSynopsis
sources

Package gofigure imports 9 packages (graph) and is imported by 11 packages. Updated 2019-06-14. Refresh now. Tools for package owners.