8 // An Environment adds additional behavior to a Fetcher, such a type conversion,
11 // `Environment`s are the primary way to communicate with various configuration
12 // sources, such as the OS environment variables, the `.gitconfig`, and even
13 // `map[string]string`s.
14 type Environment interface {
15 // Get is shorthand for calling `e.Fetcher.Get(key)`.
16 Get(key string) (val string, ok bool)
18 // Get is shorthand for calling `e.Fetcher.GetAll(key)`.
19 GetAll(key string) (vals []string)
21 // Bool returns the boolean state associated with a given key, or the
22 // value "def", if no value was associated.
24 // The "boolean state associated with a given key" is defined as the
25 // case-insensitive string comparison with the following:
28 // "true", "1", "on", "yes", or "t"
30 // "false", "0", "off", "no", "f", or otherwise.
31 Bool(key string, def bool) (val bool)
33 // Int returns the int value associated with a given key, or the value
34 // "def", if no value was associated.
36 // To convert from a the string value attached to a given key,
37 // `strconv.Atoi(val)` is called. If `Atoi` returned a non-nil error,
38 // then the value "def" will be returned instead.
40 // Otherwise, if the value was converted `string -> int` successfully,
41 // then it will be returned wholesale.
42 Int(key string, def int) (val int)
44 // All returns a copy of all the key/value pairs for the current
46 All() map[string][]string
49 type environment struct {
50 // Fetcher is the `environment`'s source of data.
54 // EnvironmentOf creates a new `Environment` initialized with the givne
56 func EnvironmentOf(f Fetcher) Environment {
57 return &environment{f}
60 func (e *environment) Get(key string) (val string, ok bool) {
61 return e.Fetcher.Get(key)
64 func (e *environment) GetAll(key string) []string {
65 return e.Fetcher.GetAll(key)
68 func (e *environment) Bool(key string, def bool) (val bool) {
69 s, _ := e.Fetcher.Get(key)
74 switch strings.ToLower(s) {
75 case "true", "1", "on", "yes", "t":
77 case "false", "0", "off", "no", "f":
84 func (e *environment) Int(key string, def int) (val int) {
85 s, _ := e.Fetcher.Get(key)
90 i, err := strconv.Atoi(s)
98 func (e *environment) All() map[string][]string {
99 return e.Fetcher.All()