env.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package env
  2. import (
  3. "os"
  4. "time"
  5. "github.com/opencost/opencost/core/pkg/util/mapper"
  6. )
  7. //--------------------------------------------------------------------------
  8. // EnvVar mapper.Map Implementation
  9. //--------------------------------------------------------------------------
  10. // envMap contains Getter and Setter implementations for environment variables
  11. type envMap struct{}
  12. func (em *envMap) Has(key string) bool {
  13. _, ok := os.LookupEnv(key)
  14. return ok
  15. }
  16. // Get returns the value for the provided environment variable
  17. func (em *envMap) Get(key string) string {
  18. return os.Getenv(key)
  19. }
  20. // Set sets the value for the provided key and returns true if successful. Otherwise,
  21. // false is returned.
  22. func (em *envMap) Set(key string, value string) error {
  23. return os.Setenv(key, value)
  24. }
  25. // This PrimitiveMapper implementation leverages os.Getenv() and os.Setenv() to get/set
  26. // primitive go values as environment variables.
  27. var envMapper mapper.PrimitiveMap = mapper.NewMapper(&envMap{})
  28. //--------------------------------------------------------------------------
  29. // Package Funcs
  30. //--------------------------------------------------------------------------
  31. // Get parses an string from the environment variable key parameter. If the environment
  32. // variable is empty, the defaultValue parameter is returned.
  33. func Get(key string, defaultValue string) string {
  34. return envMapper.Get(key, defaultValue)
  35. }
  36. // GetInt parses an int from the environment variable key parameter. If the environment
  37. // variable is empty or fails to parse, the defaultValue parameter is returned.
  38. func GetInt(key string, defaultValue int) int {
  39. return envMapper.GetInt(key, defaultValue)
  40. }
  41. // GetInt8 parses an int8 from the environment variable key parameter. If the environment
  42. // variable is empty or fails to parse, the defaultValue parameter is returned.
  43. func GetInt8(key string, defaultValue int8) int8 {
  44. return envMapper.GetInt8(key, defaultValue)
  45. }
  46. // GetInt16 parses an int16 from the environment variable key parameter. If the environment
  47. // variable is empty or fails to parse, the defaultValue parameter is returned.
  48. func GetInt16(key string, defaultValue int16) int16 {
  49. return envMapper.GetInt16(key, defaultValue)
  50. }
  51. // GetInt32 parses an int32 from the environment variable key parameter. If the environment
  52. // variable is empty or fails to parse, the defaultValue parameter is returned.
  53. func GetInt32(key string, defaultValue int32) int32 {
  54. return envMapper.GetInt32(key, defaultValue)
  55. }
  56. // GetInt64 parses an int64 from the environment variable key parameter. If the environment
  57. // variable is empty or fails to parse, the defaultValue parameter is returned.
  58. func GetInt64(key string, defaultValue int64) int64 {
  59. return envMapper.GetInt64(key, defaultValue)
  60. }
  61. // GetUInt parses a uint from the environment variable key parameter. If the environment
  62. // variable is empty or fails to parse, the defaultValue parameter is returned.
  63. func GetUInt(key string, defaultValue uint) uint {
  64. return envMapper.GetUInt(key, defaultValue)
  65. }
  66. // GetUInt8 parses a uint8 from the environment variable key parameter. If the environment
  67. // variable is empty or fails to parse, the defaultValue parameter is returned.
  68. func GetUInt8(key string, defaultValue uint8) uint8 {
  69. return envMapper.GetUInt8(key, defaultValue)
  70. }
  71. // GetUInt16 parses a uint16 from the environment variable key parameter. If the environment
  72. // variable is empty or fails to parse, the defaultValue parameter is returned.
  73. func GetUInt16(key string, defaultValue uint16) uint16 {
  74. return envMapper.GetUInt16(key, defaultValue)
  75. }
  76. // GetUInt32 parses a uint32 from the environment variable key parameter. If the environment
  77. // variable is empty or fails to parse, the defaultValue parameter is returned.
  78. func GetUInt32(key string, defaultValue uint32) uint32 {
  79. return envMapper.GetUInt32(key, defaultValue)
  80. }
  81. // GetUInt64 parses a uint64 from the environment variable key parameter. If the environment
  82. // variable is empty or fails to parse, the defaultValue parameter is returned.
  83. func GetUInt64(key string, defaultValue uint64) uint64 {
  84. return envMapper.GetUInt64(key, defaultValue)
  85. }
  86. // GetFloat32 parses a float32 from the environment variable key parameter. If the environment
  87. // variable is empty or fails to parse, the defaultValue parameter is returned.
  88. func GetFloat32(key string, defaultValue float32) float32 {
  89. return envMapper.GetFloat32(key, defaultValue)
  90. }
  91. // GetFloat64 parses a float64 from the environment variable key parameter. If the environment
  92. // variable is empty or fails to parse, the defaultValue parameter is returned.
  93. func GetFloat64(key string, defaultValue float64) float64 {
  94. return envMapper.GetFloat64(key, defaultValue)
  95. }
  96. // GetBool parses a bool from the environment variable key parameter. If the environment
  97. // variable is empty or fails to parse, the defaultValue parameter is returned.
  98. func GetBool(key string, defaultValue bool) bool {
  99. return envMapper.GetBool(key, defaultValue)
  100. }
  101. // GetDuration parses a time.Duration from the environment variable key parameter. If the environment
  102. // variable is empty or fails to parse, the defaultValue parameter is returned.
  103. func GetDuration(key string, defaultValue time.Duration) time.Duration {
  104. return envMapper.GetDuration(key, defaultValue)
  105. }
  106. // GetList parses a []string from the environment variable key parameter. If the environment
  107. // // variable is empty or fails to parse, nil is returned.
  108. func GetList(key, delimiter string) []string {
  109. return envMapper.GetList(key, delimiter)
  110. }
  111. // Set sets the environment variable for the key provided using the value provided.
  112. func Set(key string, value string) error {
  113. return envMapper.Set(key, value)
  114. }
  115. // SetInt sets the environment variable to a string formatted int value
  116. func SetInt(key string, value int) error {
  117. return envMapper.SetInt(key, value)
  118. }
  119. // SetInt8 sets the environment variable to a string formatted int8 value.
  120. func SetInt8(key string, value int8) error {
  121. return envMapper.SetInt8(key, value)
  122. }
  123. // SetInt16 sets the environment variable to a string formatted int16 value.
  124. func SetInt16(key string, value int16) error {
  125. return envMapper.SetInt16(key, value)
  126. }
  127. // SetInt32 sets the environment variable to a string formatted int32 value.
  128. func SetInt32(key string, value int32) error {
  129. return envMapper.SetInt32(key, value)
  130. }
  131. // SetInt64 sets the environment variable to a string formatted int64 value.
  132. func SetInt64(key string, value int64) error {
  133. return envMapper.SetInt64(key, value)
  134. }
  135. // SetUInt sets the environment variable to a string formatted uint value
  136. func SetUInt(key string, value uint) error {
  137. return envMapper.SetUInt(key, value)
  138. }
  139. // SetUInt8 sets the environment variable to a string formatted uint8 value
  140. func SetUInt8(key string, value uint8) error {
  141. return envMapper.SetUInt8(key, value)
  142. }
  143. // SetUInt16 sets the environment variable to a string formatted uint16 value
  144. func SetUInt16(key string, value uint16) error {
  145. return envMapper.SetUInt16(key, value)
  146. }
  147. // SetUInt32 sets the environment variable to a string formatted uint32 value
  148. func SetUInt32(key string, value uint32) error {
  149. return envMapper.SetUInt32(key, value)
  150. }
  151. // SetUInt64 sets the environment variable to a string formatted uint64 value
  152. func SetUInt64(key string, value uint64) error {
  153. return envMapper.SetUInt64(key, value)
  154. }
  155. // SetBool sets the environment variable to a string formatted bool value.
  156. func SetBool(key string, value bool) error {
  157. return envMapper.SetBool(key, value)
  158. }
  159. // SetDuration sets the environment variable to a string formatted time.Duration
  160. func SetDuration(key string, value time.Duration) error {
  161. return envMapper.SetDuration(key, value)
  162. }
  163. // GetPrefixInt parses an int from the environment variable key parameter. It first checks the env var with the prefix
  164. // then checks the env var without the prefix If the environment variable is empty or fails to parse, the defaultValue
  165. // parameter is returned.
  166. func GetPrefixInt(prefix, key string, defaultValue int) int {
  167. return envMapper.GetInt(prefix+key, envMapper.GetInt(key, defaultValue))
  168. }