Documentation ¶
Overview ¶
Package copperhead provides a configuration loader that can load configuration from environment, files, or byte slices.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config encapsulates configuration loading.
Example ¶
ExampleConfig is a testable example for Copperhead
package main import ( "fmt" "os" "github.com/Sydsvenskan/copperhead" yaml "gopkg.in/yaml.v2" ) // Configuration is an example configuration struct type Configuration struct { Name string WD string URL *copperhead.URL FileURL *copperhead.URL Birdie *ExampleNest } // ExampleNest is an exampe of a nested configuration struct. type ExampleNest struct { Name string Value int64 YamlIT string `yaml:"yaml_it"` ComplexEnv int } // ExampleConfig is a testable example for Copperhead func main() { os.Setenv("APP_NAME", "example-app") os.Setenv("APP_URL", "https://www.example.com") os.Setenv("APP_BIRD", "12") os.Setenv("DUMB_USAGE", `{"ComplexEnv":42}`) cfg := Configuration{ WD: "with defaults", } ch, err := copperhead.New(&cfg, copperhead.WithConfigurationFile( "./test-data/example.conf", copperhead.FileRequired, nil, ), copperhead.WithConfigurationFile( "./test-data/example.conf.yaml", copperhead.FileRequired, copperhead.UnmarshalerFunc(yaml.Unmarshal), ), copperhead.WithEnvironment(map[string]string{ "Name": "APP_NAME", "URL": "APP_URL", "Birdie.Value": "APP_BIRD", "Birdie": "DUMB_USAGE", }), ) if err != nil { println(err.Error()) os.Exit(1) } fmt.Println("Name:", cfg.Name) fmt.Println("WD:", cfg.WD) fmt.Println("URL:", cfg.URL.String()) fmt.Println("URL proto:", cfg.URL.Scheme) fmt.Println("Birdie.Name:", cfg.Birdie.Name) fmt.Println("Birdie.Value:", cfg.Birdie.Value) fmt.Println("Birdie.YamlIT:", cfg.Birdie.YamlIT) fmt.Println("Birdie.ComplexEnv:", cfg.Birdie.ComplexEnv) // Load additional config file. err = ch.File("./test-data/file-url.json", copperhead.FileRequired, nil) if err != nil { println(err.Error()) os.Exit(1) } fmt.Println("File URL (json):", cfg.FileURL.String()) err = ch.File("./test-data/file-url.yaml", copperhead.FileRequired, copperhead.UnmarshalerFunc(yaml.Unmarshal)) if err != nil { println(err.Error()) os.Exit(1) } fmt.Println("File URL (yaml):", cfg.FileURL.String()) }
Output: Name: example-app WD: with defaults URL: https://www.example.com URL proto: https Birdie.Name: Heron Birdie.Value: 12 Birdie.YamlIT: Hello from YAML Birdie.ComplexEnv: 42 File URL (json): http://www.example.com/json File URL (yaml): http://www.example.com/yaml
func (*Config) Data ¶
func (c *Config) Data(data []byte, unm Unmarshaler) error
Data reads the provided configuration data.
func (*Config) Environment ¶
Environment populates our configuration with environment variables.
func (*Config) File ¶
func (c *Config) File(filename string, mode FileMode, unm Unmarshaler) error
File reads configuration from a file.
type Duration ¶ added in v1.2.0
Duration is an TextUnmarshaler-aware Duration
func (*Duration) UnmarshalText ¶ added in v1.2.0
UnmarshalText implements encoding.TextUnmarshaler.
type FileMode ¶
type FileMode string
FileMode controls file loading behaviour.
const ( FileRequired FileMode = "required" FileOptional = "optional" )
The file loading modes
type Option ¶
Option configures our... inception!
func WithConfigurationData ¶
func WithConfigurationData(data []byte, unm Unmarshaler) Option
WithConfigurationData reads the provided configuration data.
func WithConfigurationFile ¶
func WithConfigurationFile(filename string, mode FileMode, unm Unmarshaler) Option
WithConfigurationFile reads configuration from a file.
func WithEnvironment ¶
WithEnvironment bootstraps our configuration with environment variables.
type Time ¶ added in v1.2.0
Time is an TextUnmarshaler-aware Time
func (*Time) UnmarshalText ¶ added in v1.2.0
UnmarshalText implements encoding.TextUnmarshaler.
type URL ¶ added in v1.1.0
URL is an TextUnmarshaler-aware URL
func MustParseURL ¶ added in v1.3.0
MustParseURL is a helper function for setting configuration defaults. Panics if the passed url is invalid.
func (*URL) UnmarshalText ¶ added in v1.1.0
UnmarshalText implements encoding.TextUnmarshaler.
type Unmarshaler ¶
Unmarshaler is something that can unmarshal a configuration.
type UnmarshalerFunc ¶
UnmarshalerFunc is an unmarshal function
func (UnmarshalerFunc) Unmarshal ¶
func (uf UnmarshalerFunc) Unmarshal( data []byte, v interface{}, ) error
Unmarshal the config data.