penv.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package penv
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "reflect"
  7. "strconv"
  8. "strings"
  9. "golang.org/x/text/cases"
  10. "golang.org/x/text/language"
  11. )
  12. const NoDescription = "No description"
  13. var Prefix = "ENV_"
  14. type DumpVar struct {
  15. Name string
  16. NameEnv string
  17. NameFlag string
  18. Desc string
  19. Type string
  20. Value string
  21. Secret bool
  22. }
  23. func generate(name string) string {
  24. n := ""
  25. prevBig := true
  26. for i, char := range name {
  27. if i > 0 && string(char) == strings.ToUpper(string(char)) && !prevBig {
  28. n += "_"
  29. }
  30. if string(char) == strings.ToUpper(string(char)) {
  31. prevBig = true
  32. } else {
  33. prevBig = false
  34. }
  35. n += string(char)
  36. }
  37. return n
  38. }
  39. func generateEnvName(name string) string {
  40. return strings.ToUpper(Prefix + generate(name))
  41. }
  42. func generateFlagName(name string) string {
  43. return strings.ToLower(generate(name))
  44. }
  45. func stringToInt(value string) (int, error) {
  46. return strconv.Atoi(value)
  47. }
  48. func stringToInt64(value string) (int64, error) {
  49. return strconv.ParseInt(value, 10, 64)
  50. }
  51. func DumpConfig(config any) map[string]DumpVar {
  52. res := map[string]DumpVar{}
  53. v := reflect.ValueOf(config).Elem()
  54. t := v.Type()
  55. for i := 0; i < t.NumField(); i++ {
  56. nameEnv := generateEnvName(t.Field(i).Name)
  57. fieldType := t.Field(i).Type.Kind().String()
  58. nameFlag := generateFlagName(t.Field(i).Name)
  59. description := t.Field(i).Tag.Get("description")
  60. if description == "" {
  61. description = NoDescription
  62. }
  63. secret := t.Field(i).Tag.Get("secret")
  64. if fieldType == "string" {
  65. res[t.Field(i).Name] = DumpVar{
  66. Name: t.Field(i).Name,
  67. NameEnv: nameEnv,
  68. NameFlag: nameFlag,
  69. Desc: description,
  70. Type: cases.Title(language.English).String(fieldType),
  71. Value: *v.Field(i).Addr().Interface().(*string),
  72. Secret: secret == "1" || secret == "true",
  73. }
  74. } else if fieldType == "int" {
  75. res[t.Field(i).Name] = DumpVar{
  76. Name: t.Field(i).Name,
  77. NameEnv: nameEnv,
  78. NameFlag: nameFlag,
  79. Desc: description,
  80. Type: cases.Title(language.English).String(fieldType),
  81. Value: fmt.Sprintf("%d", *v.Field(i).Addr().Interface().(*int)),
  82. Secret: secret == "1" || secret == "true",
  83. }
  84. } else if fieldType == "int64" {
  85. res[t.Field(i).Name] = DumpVar{
  86. Name: t.Field(i).Name,
  87. NameEnv: nameEnv,
  88. NameFlag: nameFlag,
  89. Desc: description,
  90. Type: cases.Title(language.English).String(fieldType),
  91. Value: fmt.Sprintf("%d", *v.Field(i).Addr().Interface().(*int64)),
  92. Secret: secret == "1" || secret == "true",
  93. }
  94. }
  95. }
  96. return res
  97. }
  98. // ProcessConfig automaticaly read flags and ENVs to structure.
  99. //
  100. // config - must be a pointer to structure
  101. //
  102. // var Config struct {
  103. // Deployment string `default:"development"`
  104. // Host string `default:"127.0.0.1"`
  105. // Port string `default:"8080"`
  106. // }
  107. //
  108. // func init() {
  109. // if err := penv.ProcessConfig(&Config); err != nil {
  110. // panic(err)
  111. // }
  112. // }
  113. func ProcessConfig(config any) error {
  114. v := reflect.ValueOf(config).Elem()
  115. t := v.Type()
  116. // Flags
  117. for i := 0; i < t.NumField(); i++ {
  118. nameEnv := generateEnvName(t.Field(i).Name)
  119. nameFlag := generateFlagName(t.Field(i).Name)
  120. fieldType := t.Field(i).Type.Kind().String()
  121. defvalue := t.Field(i).Tag.Get("default")
  122. description := t.Field(i).Tag.Get("description")
  123. if description == "" {
  124. description = NoDescription
  125. }
  126. if fieldType == "string" {
  127. value := v.Field(i).Addr().Interface().(*string)
  128. flag.StringVar(value, nameFlag, defvalue, "Or "+nameEnv+": "+description)
  129. } else if fieldType == "int" {
  130. if ndefvalue, err := stringToInt(defvalue); err == nil {
  131. value := v.Field(i).Addr().Interface().(*int)
  132. flag.IntVar(value, nameFlag, ndefvalue, "Or "+nameEnv+": "+description)
  133. } else {
  134. return err
  135. }
  136. } else if fieldType == "int64" {
  137. if ndefvalue, err := stringToInt64(defvalue); err == nil {
  138. value := v.Field(i).Addr().Interface().(*int64)
  139. flag.Int64Var(value, nameFlag, ndefvalue, "Or "+nameEnv+": "+description)
  140. } else {
  141. return err
  142. }
  143. }
  144. }
  145. flag.Parse()
  146. // ENVs
  147. for i := 0; i < t.NumField(); i++ {
  148. nameEnv := generateEnvName(t.Field(i).Name)
  149. fieldType := t.Field(i).Type.Kind().String()
  150. if os.Getenv(nameEnv) != "" {
  151. if fieldType == "string" {
  152. value := v.Field(i).Addr().Interface().(*string)
  153. *value = os.Getenv(nameEnv)
  154. } else if fieldType == "int" {
  155. if nvalue, err := stringToInt(os.Getenv(nameEnv)); err == nil {
  156. value := v.Field(i).Addr().Interface().(*int)
  157. *value = nvalue
  158. } else {
  159. return err
  160. }
  161. } else if fieldType == "int64" {
  162. if nvalue, err := stringToInt64(os.Getenv(nameEnv)); err == nil {
  163. value := v.Field(i).Addr().Interface().(*int64)
  164. *value = nvalue
  165. } else {
  166. return err
  167. }
  168. }
  169. }
  170. }
  171. return nil
  172. }