import "github.com/ian-kent/gofigure"
Package gofigure simplifies configuration of Go applications.
Define a struct and call Gofigure()
Debug controls log output
DefaultOrder sets the default order used
ErrInvalidOrder is returned if the "order" struct tag is invalid
ErrUnsupportedFieldType is returned for unsupported field types, e.g. chan or func
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
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)
Path | Synopsis |
---|---|
sources |
Package gofigure imports 9 packages (graph) and is imported by 11 packages. Updated 2019-06-14. Refresh now. Tools for package owners.