env.go 7.2 KB

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