env.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package env
  2. import (
  3. "os"
  4. "strconv"
  5. )
  6. // Get parses an string from the environment variable key parameter. If the environment
  7. // variable is empty, the defaultValue parameter is returned.
  8. func Get(key string, defaultValue string) string {
  9. r := os.Getenv(key)
  10. if r == "" {
  11. return defaultValue
  12. }
  13. return r
  14. }
  15. // GetInt parses an int from the environment variable key parameter. If the environment
  16. // variable is empty or fails to parse, the defaultValue parameter is returned.
  17. func GetInt(key string, defaultValue int) int {
  18. r := os.Getenv(key)
  19. i, err := strconv.Atoi(r)
  20. if err != nil {
  21. return defaultValue
  22. }
  23. return i
  24. }
  25. // GetInt8 parses an int8 from the environment variable key parameter. If the environment
  26. // variable is empty or fails to parse, the defaultValue parameter is returned.
  27. func GetInt8(key string, defaultValue int8) int8 {
  28. r := os.Getenv(key)
  29. i, err := strconv.ParseInt(r, 10, 8)
  30. if err != nil {
  31. return defaultValue
  32. }
  33. return int8(i)
  34. }
  35. // GetInt16 parses an int16 from the environment variable key parameter. If the environment
  36. // variable is empty or fails to parse, the defaultValue parameter is returned.
  37. func GetInt16(key string, defaultValue int16) int16 {
  38. r := os.Getenv(key)
  39. i, err := strconv.ParseInt(r, 10, 16)
  40. if err != nil {
  41. return defaultValue
  42. }
  43. return int16(i)
  44. }
  45. // GetInt32 parses an int32 from the environment variable key parameter. If the environment
  46. // variable is empty or fails to parse, the defaultValue parameter is returned.
  47. func GetInt32(key string, defaultValue int32) int32 {
  48. r := os.Getenv(key)
  49. i, err := strconv.ParseInt(r, 10, 32)
  50. if err != nil {
  51. return defaultValue
  52. }
  53. return int32(i)
  54. }
  55. // GetInt64 parses an int64 from the environment variable key parameter. If the environment
  56. // variable is empty or fails to parse, the defaultValue parameter is returned.
  57. func GetInt64(key string, defaultValue int64) int64 {
  58. r := os.Getenv(key)
  59. i, err := strconv.ParseInt(r, 10, 64)
  60. if err != nil {
  61. return defaultValue
  62. }
  63. return i
  64. }
  65. // GetUInt parses a uint from the environment variable key parameter. If the environment
  66. // variable is empty or fails to parse, the defaultValue parameter is returned.
  67. func GetUInt(key string, defaultValue uint) uint {
  68. r := os.Getenv(key)
  69. i, err := strconv.ParseUint(r, 10, 32)
  70. if err != nil {
  71. return defaultValue
  72. }
  73. return uint(i)
  74. }
  75. // GetUInt8 parses a uint8 from the environment variable key parameter. If the environment
  76. // variable is empty or fails to parse, the defaultValue parameter is returned.
  77. func GetUInt8(key string, defaultValue uint8) uint8 {
  78. r := os.Getenv(key)
  79. i, err := strconv.ParseUint(r, 10, 8)
  80. if err != nil {
  81. return defaultValue
  82. }
  83. return uint8(i)
  84. }
  85. // GetUInt16 parses a uint16 from the environment variable key parameter. If the environment
  86. // variable is empty or fails to parse, the defaultValue parameter is returned.
  87. func GetUInt16(key string, defaultValue uint16) uint16 {
  88. r := os.Getenv(key)
  89. i, err := strconv.ParseUint(r, 10, 16)
  90. if err != nil {
  91. return defaultValue
  92. }
  93. return uint16(i)
  94. }
  95. // GetUInt32 parses a uint32 from the environment variable key parameter. If the environment
  96. // variable is empty or fails to parse, the defaultValue parameter is returned.
  97. func GetUInt32(key string, defaultValue uint32) uint32 {
  98. r := os.Getenv(key)
  99. i, err := strconv.ParseUint(r, 10, 32)
  100. if err != nil {
  101. return defaultValue
  102. }
  103. return uint32(i)
  104. }
  105. // GetUInt64 parses a uint64 from the environment variable key parameter. If the environment
  106. // variable is empty or fails to parse, the defaultValue parameter is returned.
  107. func GetUInt64(key string, defaultValue uint64) uint64 {
  108. r := os.Getenv(key)
  109. i, err := strconv.ParseUint(r, 10, 64)
  110. if err != nil {
  111. return defaultValue
  112. }
  113. return uint64(i)
  114. }
  115. // GetFloat32 parses a float32 from the environment variable key parameter. If the environment
  116. // variable is empty or fails to parse, the defaultValue parameter is returned.
  117. func GetFloat32(key string, defaultValue float32) float32 {
  118. r := os.Getenv(key)
  119. f, err := strconv.ParseFloat(r, 32)
  120. if err != nil {
  121. return defaultValue
  122. }
  123. return float32(f)
  124. }
  125. // GetFloat64 parses a float64 from the environment variable key parameter. If the environment
  126. // variable is empty or fails to parse, the defaultValue parameter is returned.
  127. func GetFloat64(key string, defaultValue float64) float64 {
  128. r := os.Getenv(key)
  129. f, err := strconv.ParseFloat(r, 64)
  130. if err != nil {
  131. return defaultValue
  132. }
  133. return f
  134. }
  135. // GetBool parses a bool from the environment variable key parameter. If the environment
  136. // variable is empty or fails to parse, the defaultValue parameter is returned.
  137. func GetBool(key string, defaultValue bool) bool {
  138. r := os.Getenv(key)
  139. b, err := strconv.ParseBool(r)
  140. if err != nil {
  141. return defaultValue
  142. }
  143. return b
  144. }
  145. // Set sets the environment variable for the key provided using the value provided.
  146. func Set(key string, value string) error {
  147. return os.Setenv(key, value)
  148. }
  149. // SetInt sets the environment variable to a string formatted int value
  150. func SetInt(key string, value int) error {
  151. return os.Setenv(key, strconv.Itoa(value))
  152. }
  153. // SetInt8 sets the environment variable to a string formatted int8 value.
  154. func SetInt8(key string, value int8) error {
  155. return os.Setenv(key, strconv.FormatInt(int64(value), 10))
  156. }
  157. // SetInt16 sets the environment variable to a string formatted int16 value.
  158. func SetInt16(key string, value int16) error {
  159. return os.Setenv(key, strconv.FormatInt(int64(value), 10))
  160. }
  161. // SetInt32 sets the environment variable to a string formatted int32 value.
  162. func SetInt32(key string, value int32) error {
  163. return os.Setenv(key, strconv.FormatInt(int64(value), 10))
  164. }
  165. // SetInt64 sets the environment variable to a string formatted int64 value.
  166. func SetInt64(key string, value int64) error {
  167. return os.Setenv(key, strconv.FormatInt(value, 10))
  168. }
  169. // SetUInt sets the environment variable to a string formatted uint value
  170. func SetUInt(key string, value uint) error {
  171. return os.Setenv(key, strconv.FormatUint(uint64(value), 10))
  172. }
  173. // SetUInt8 sets the environment variable to a string formatted uint8 value
  174. func SetUInt8(key string, value uint8) error {
  175. return os.Setenv(key, strconv.FormatUint(uint64(value), 10))
  176. }
  177. // SetUInt16 sets the environment variable to a string formatted uint16 value
  178. func SetUInt16(key string, value uint16) error {
  179. return os.Setenv(key, strconv.FormatUint(uint64(value), 10))
  180. }
  181. // SetUInt32 sets the environment variable to a string formatted uint32 value
  182. func SetUInt32(key string, value uint32) error {
  183. return os.Setenv(key, strconv.FormatUint(uint64(value), 10))
  184. }
  185. // SetUInt64 sets the environment variable to a string formatted uint64 value
  186. func SetUInt64(key string, value uint64) error {
  187. return os.Setenv(key, strconv.FormatUint(value, 10))
  188. }
  189. // SetBool sets the environment variable to a string formatted bool value.
  190. func SetBool(key string, value bool) error {
  191. return os.Setenv(key, strconv.FormatBool(value))
  192. }