env.go 6.7 KB

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