github.com/stffabi/git-lfs@v2.3.5-0.20180214015214-8eeaa8d88902+incompatible/config/environment.go (about) 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 // All returns a copy of all the key/value pairs for the current 45 // environment. 46 All() map[string][]string 47 } 48 49 type environment struct { 50 // Fetcher is the `environment`'s source of data. 51 Fetcher Fetcher 52 } 53 54 // EnvironmentOf creates a new `Environment` initialized with the givne 55 // `Fetcher`, "f". 56 func EnvironmentOf(f Fetcher) Environment { 57 return &environment{f} 58 } 59 60 func (e *environment) Get(key string) (val string, ok bool) { 61 return e.Fetcher.Get(key) 62 } 63 64 func (e *environment) GetAll(key string) []string { 65 return e.Fetcher.GetAll(key) 66 } 67 68 func (e *environment) Bool(key string, def bool) bool { 69 s, _ := e.Fetcher.Get(key) 70 return Bool(s, def) 71 } 72 73 func (e *environment) Int(key string, def int) int { 74 s, _ := e.Fetcher.Get(key) 75 return Int(s, def) 76 } 77 78 func (e *environment) All() map[string][]string { 79 return e.Fetcher.All() 80 } 81 82 // Int returns the int value associated with the given value, or the value 83 // "def", if the value is blank. 84 // 85 // To convert from a the string value attached to a given key, 86 // `strconv.Atoi(val)` is called. If `Atoi` returned a non-nil error, 87 // then the value "def" will be returned instead. 88 // 89 // Otherwise, if the value was converted `string -> int` successfully, 90 // then it will be returned wholesale. 91 func Int(value string, def int) int { 92 if len(value) == 0 { 93 return def 94 } 95 96 i, err := strconv.Atoi(value) 97 if err != nil { 98 return def 99 } 100 101 return i 102 } 103 104 // Bool returns the boolean state associated with the given value, or the 105 // value "def", if the value is blank. 106 // 107 // The "boolean state associated with a given key" is defined as the 108 // case-insensitive string comparison with the following: 109 // 110 // 1) true if... 111 // "true", "1", "on", "yes", or "t" 112 // 2) false if... 113 // "false", "0", "off", "no", "f", or otherwise. 114 func Bool(value string, def bool) bool { 115 if len(value) == 0 { 116 return def 117 } 118 119 switch strings.ToLower(value) { 120 case "true", "1", "on", "yes", "t": 121 return true 122 case "false", "0", "off", "no", "f": 123 return false 124 default: 125 return false 126 } 127 }