| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | // An Environment adds additional behavior to a Fetcher, such a type conversion, |
| 9 | // and default values. |
| 10 | // |
| 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) |
| 17 | |
| 18 | // Get is shorthand for calling `e.Fetcher.GetAll(key)`. |
| 19 | GetAll(key string) (vals []string) |
| 20 | |
| 21 | // Bool returns the boolean state associated with a given key, or the |
| 22 | // value "def", if no value was associated. |
| 23 | // |
| 24 | // The "boolean state associated with a given key" is defined as the |
| 25 | // case-insensitive string comparison with the following: |
| 26 | // |
| 27 | // 1) true if... |
| 28 | // "true", "1", "on", "yes", or "t" |
| 29 | // 2) false if... |
| 30 | // "false", "0", "off", "no", "f", or otherwise. |
| 31 | Bool(key string, def bool) (val bool) |
| 32 | |
| 33 | // Int returns the int value associated with a given key, or the value |
| 34 | // "def", if no value was associated. |
| 35 | // |
| 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. |
| 39 | // |
| 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) |
| 43 | |
| 44 | // Int64 returns the int64 value associated with a given key, or the value |
| 45 | // "def", if no value was associated. |
| 46 | // |
| 47 | // To convert from the string value attached to a given key, |
| 48 | // `strconv.ParseInt(val, 10, 64)` is called. If `ParseInt` returned a |
| 49 | // non-nil error, then the value "def" will be returned instead. |
| 50 | // |
| 51 | // Otherwise, if the value was converted `string -> int64` successfully, |
| 52 | // then it will be returned wholesale. |
| 53 | Int64(key string, def int64) (val int64) |
| 54 | |
| 55 | // All returns a copy of all the key/value pairs for the current |
| 56 | // environment. |
| 57 | All() map[string][]string |
| 58 | } |
| 59 | |
| 60 | type environment struct { |
| 61 | // Fetcher is the `environment`'s source of data. |
| 62 | Fetcher Fetcher |
| 63 | } |
| 64 | |
| 65 | // EnvironmentOf creates a new `Environment` initialized with the givne |
| 66 | // `Fetcher`, "f". |
| 67 | func EnvironmentOf(f Fetcher) Environment { |
| 68 | return &environment{f} |
| 69 | } |
| 70 | |
| 71 | func (e *environment) Get(key string) (val string, ok bool) { |
| 72 | return e.Fetcher.Get(key) |
| 73 | } |
| 74 | |
| 75 | func (e *environment) GetAll(key string) []string { |
| 76 | return e.Fetcher.GetAll(key) |
| 77 | } |
| 78 | |
| 79 | func (e *environment) Bool(key string, def bool) bool { |
| 80 | s, _ := e.Fetcher.Get(key) |
| 81 | return Bool(s, def) |
| 82 | } |
| 83 | |
| 84 | func (e *environment) Int(key string, def int) int { |
| 85 | s, _ := e.Fetcher.Get(key) |
| 86 | return Int(s, def) |
| 87 | } |
| 88 | |
| 89 | func (e *environment) Int64(key string, def int64) int64 { |
| 90 | s, _ := e.Fetcher.Get(key) |
| 91 | return Int64(s, def) |
| 92 | } |
| 93 | |
| 94 | func (e *environment) All() map[string][]string { |
| 95 | return e.Fetcher.All() |
| 96 | } |
| 97 | |
| 98 | // Int returns the int value associated with the given value, or the value |
| 99 | // "def", if the value is blank. |
| 100 | // |
| 101 | // To convert from a the string value attached to a given key, |
| 102 | // `strconv.Atoi(val)` is called. If `Atoi` returned a non-nil error, |
| 103 | // then the value "def" will be returned instead. |
| 104 | // |
| 105 | // Otherwise, if the value was converted `string -> int` successfully, |
| 106 | // then it will be returned wholesale. |
| 107 | func Int(value string, def int) int { |
| 108 | if len(value) == 0 { |
| 109 | return def |
| 110 | } |
| 111 | |
| 112 | i, err := strconv.Atoi(value) |
| 113 | if err != nil { |
| 114 | return def |
| 115 | } |
| 116 | |
| 117 | return i |
| 118 | } |
| 119 | |
| 120 | // Int64 returns the int64 value associated with the given value, or the value |
| 121 | // "def", if the value is blank. |
| 122 | // |
| 123 | // To convert from the string value attached to a given key, |
| 124 | // `strconv.ParseInt(val, 10, 64)` is called. If `ParseInt` returned a |
| 125 | // non-nil error, then the value "def" will be returned instead. |
| 126 | // |
| 127 | // Otherwise, if the value was converted `string -> int64` successfully, |
| 128 | // then it will be returned wholesale. |
| 129 | func Int64(value string, def int64) int64 { |
| 130 | if len(value) == 0 { |
| 131 | return def |
| 132 | } |
| 133 | |
| 134 | i, err := strconv.ParseInt(value, 10, 64) |
| 135 | if err != nil { |
| 136 | return def |
| 137 | } |
| 138 | |
| 139 | return i |
| 140 | } |
| 141 | |
| 142 | // Bool returns the boolean state associated with the given value, or the |
| 143 | // value "def", if the value is blank. |
| 144 | // |
| 145 | // The "boolean state associated with a given key" is defined as the |
| 146 | // case-insensitive string comparison with the following: |
| 147 | // |
| 148 | // 1. true if... |
| 149 | // "true", "1", "on", "yes", or "t" |
| 150 | // 2. false if... |
| 151 | // "false", "0", "off", "no", "f", or otherwise. |
| 152 | func Bool(value string, def bool) bool { |
| 153 | if len(value) == 0 { |
| 154 | return def |
| 155 | } |
| 156 | |
| 157 | switch strings.ToLower(value) { |
| 158 | case "true", "1", "on", "yes", "t": |
| 159 | return true |
| 160 | case "false", "0", "off", "no", "f": |
| 161 | return false |
| 162 | default: |
| 163 | return false |
| 164 | } |
| 165 | } |