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.
Click to show internal directories.
Click to hide internal directories.