Documentation
¶
Index ¶
Examples ¶
Constants ¶
const (
DefaultConfigKeyName = "config"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config interface {
Bool(key string) bool
Bools(key string) []bool
BoolMap(key string) map[string]bool
Duration(key string) time.Duration
Float64(key string) float64
Float64s(key string) []float64
Float64Map(key string) map[string]float64
Int(key string) int
Ints(key string) []int
IntMap(key string) map[string]int
Int64(key string) int64
Int64s(key string) []int64
Int64Map(key string) map[string]int64
String(key string) string
Strings(key string) []string
StringMap(key string) map[string]string
}
Config represents a configuration source.
func LoadConfig ¶
Returns a Config by reading a YAML based configuration file, environment variables and command line flags.
The configuration file is loaded based on the WithConfigKeyName or the default of DefaultConfigKeyName being set in the provided pflag.FlagSet. Loading of a configuration file can be disabled via WithoutConfigurationFile, via WithConfigKeyName as a blank string or loading will be skipped if the value from the config key name is blank or unset.
If WithEnvPrefix is provided then enviroment variables prefixed with "PREFIX_" will be included in the configuration.
Example (WithConfig) ¶
package main
import (
"fmt"
"github.com/andrewheberle/configger"
"github.com/spf13/pflag"
)
func main() {
// set up a flagset and parse flags
f := pflag.NewFlagSet("example", pflag.ContinueOnError)
f.String("config", "", "path to configuration file")
f.String("foo", "default-foo", "foo flag")
f.String("bar", "default-bar", "bar flag")
f.StringSlice("baz", []string{"default-baz"}, "baz flag")
if err := f.Parse([]string{"--config", "testdata/config.yml"}); err != nil {
panic(err)
}
config, err := configger.LoadConfig(f)
if err != nil {
panic(err)
}
baz := config.Strings("baz")
fmt.Printf("foo: %s; bar: %s; baz: %s; len(baz): %d\n", config.String("foo"), config.String("bar"), baz, len(baz))
}
Output: foo: file-foo; bar: file-bar; baz: [default-baz]; len(baz): 1
Example (WithoutConfig) ¶
package main
import (
"fmt"
"github.com/andrewheberle/configger"
"github.com/spf13/pflag"
)
func main() {
// set up a flagset and parse flags
f := pflag.NewFlagSet("example", pflag.ContinueOnError)
f.String("config", "", "path to configuration file")
f.String("foo", "default-foo", "foo flag")
f.String("bar", "default-bar", "bar flag")
f.StringSlice("baz", []string{"default-baz"}, "baz flag")
if err := f.Parse([]string{"--foo", "flag-foo"}); err != nil {
panic(err)
}
config, err := configger.LoadConfig(f)
if err != nil {
panic(err)
}
baz := config.Strings("baz")
fmt.Printf("foo: %s; bar: %s; baz: %s; len(baz): %d\n", config.String("foo"), config.String("bar"), baz, len(baz))
}
Output: foo: flag-foo; bar: default-bar; baz: [default-baz]; len(baz): 1
type Option ¶
type Option func(*options)
Option is used to modify the behaviour of LoadConfig.
func WithConfigKeyName ¶
Sets a specific key name to lookup the configuration file name from.
func WithEnvPrefix ¶
Set the prefix for environment variable loading.
Example ¶
package main
import (
"fmt"
"os"
"github.com/andrewheberle/configger"
"github.com/spf13/pflag"
)
func main() {
// set up a flagset and parse flags
f := pflag.NewFlagSet("example", pflag.ContinueOnError)
f.String("config", "", "path to configuration file")
f.String("foo", "default-foo", "foo flag")
f.String("bar", "default-bar", "bar flag")
f.StringSlice("baz", []string{"default-baz"}, "baz flag")
if err := f.Parse([]string{"--baz", "flag-baz-a,flag-baz-b"}); err != nil {
panic(err)
}
// set an env var
if err := os.Setenv("TEST_FOO", "env-foo"); err != nil {
panic(err)
}
defer func() {
_ = os.Unsetenv("TEST_FOO")
}()
// load config
config, err := configger.LoadConfig(f, configger.WithEnvPrefix("test"))
if err != nil {
panic(err)
}
baz := config.Strings("baz")
fmt.Printf("foo: %s; bar: %s; baz: %s; len(baz): %d\n", config.String("foo"), config.String("bar"), baz, len(baz))
}
Output: foo: env-foo; bar: default-bar; baz: [flag-baz-a flag-baz-b]; len(baz): 2
func WithoutConfigurationFile ¶
func WithoutConfigurationFile() Option
Explicitly disable configuration file loading.