env.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package env
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "strings"
  9. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/internal/version"
  10. sortedmap "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/pkg/collection"
  11. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/pkg/protoc"
  12. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/pkg/protocgengo"
  13. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/pkg/protocgengogrpc"
  14. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/pathx"
  15. )
  16. var goctlEnv *sortedmap.SortedMap
  17. const (
  18. GoctlOS = "GOCTL_OS"
  19. GoctlArch = "GOCTL_ARCH"
  20. GoctlHome = "GOCTL_HOME"
  21. GoctlDebug = "GOCTL_DEBUG"
  22. GoctlCache = "GOCTL_CACHE"
  23. GoctlVersion = "GOCTL_VERSION"
  24. GoctlExperimental = "GOCTL_EXPERIMENTAL"
  25. ProtocVersion = "PROTOC_VERSION"
  26. ProtocGenGoVersion = "PROTOC_GEN_GO_VERSION"
  27. ProtocGenGoGRPCVersion = "PROTO_GEN_GO_GRPC_VERSION"
  28. envFileDir = "env"
  29. ExperimentalOn = "on"
  30. ExperimentalOff = "off"
  31. )
  32. // init initializes the goctlwt environment variables, the environment variables of the function are set in order,
  33. // please do not change the logic order of the code.
  34. func init() {
  35. defaultGoctlHome, err := pathx.GetDefaultGoctlHome()
  36. if err != nil {
  37. log.Fatalln(err)
  38. }
  39. goctlEnv = sortedmap.New()
  40. goctlEnv.SetKV(GoctlOS, runtime.GOOS)
  41. goctlEnv.SetKV(GoctlArch, runtime.GOARCH)
  42. existsEnv := readEnv(defaultGoctlHome)
  43. if existsEnv != nil {
  44. goctlHome, ok := existsEnv.GetString(GoctlHome)
  45. if ok && len(goctlHome) > 0 {
  46. goctlEnv.SetKV(GoctlHome, goctlHome)
  47. }
  48. if debug := existsEnv.GetOr(GoctlDebug, "").(string); debug != "" {
  49. if strings.EqualFold(debug, "true") || strings.EqualFold(debug, "false") {
  50. goctlEnv.SetKV(GoctlDebug, debug)
  51. }
  52. }
  53. if value := existsEnv.GetStringOr(GoctlCache, ""); value != "" {
  54. goctlEnv.SetKV(GoctlCache, value)
  55. }
  56. experimental := existsEnv.GetOr(GoctlExperimental, ExperimentalOff)
  57. goctlEnv.SetKV(GoctlExperimental, experimental)
  58. }
  59. if !goctlEnv.HasKey(GoctlHome) {
  60. goctlEnv.SetKV(GoctlHome, defaultGoctlHome)
  61. }
  62. if !goctlEnv.HasKey(GoctlDebug) {
  63. goctlEnv.SetKV(GoctlDebug, "False")
  64. }
  65. if !goctlEnv.HasKey(GoctlCache) {
  66. cacheDir, _ := pathx.GetCacheDir()
  67. goctlEnv.SetKV(GoctlCache, cacheDir)
  68. }
  69. if !goctlEnv.HasKey(GoctlExperimental) {
  70. goctlEnv.SetKV(GoctlExperimental, ExperimentalOff)
  71. }
  72. goctlEnv.SetKV(GoctlVersion, version.BuildVersion)
  73. protocVer, _ := protoc.Version()
  74. goctlEnv.SetKV(ProtocVersion, protocVer)
  75. protocGenGoVer, _ := protocgengo.Version()
  76. goctlEnv.SetKV(ProtocGenGoVersion, protocGenGoVer)
  77. protocGenGoGrpcVer, _ := protocgengogrpc.Version()
  78. goctlEnv.SetKV(ProtocGenGoGRPCVersion, protocGenGoGrpcVer)
  79. }
  80. func Print(args ...string) string {
  81. if len(args) == 0 {
  82. return strings.Join(goctlEnv.Format(), "\n")
  83. }
  84. var values []string
  85. for _, key := range args {
  86. value, ok := goctlEnv.GetString(key)
  87. if !ok {
  88. value = fmt.Sprintf("%s=%%not found%%", key)
  89. }
  90. values = append(values, fmt.Sprintf("%s=%s", key, value))
  91. }
  92. return strings.Join(values, "\n")
  93. }
  94. func Get(key string) string {
  95. return GetOr(key, "")
  96. }
  97. func GetOr(key, def string) string {
  98. return goctlEnv.GetStringOr(key, def)
  99. }
  100. func UseExperimental() bool {
  101. return GetOr(GoctlExperimental, ExperimentalOff) == ExperimentalOn
  102. }
  103. func readEnv(goctlHome string) *sortedmap.SortedMap {
  104. envFile := filepath.Join(goctlHome, envFileDir)
  105. data, err := os.ReadFile(envFile)
  106. if err != nil {
  107. return nil
  108. }
  109. dataStr := string(data)
  110. lines := strings.Split(dataStr, "\n")
  111. sm := sortedmap.New()
  112. for _, line := range lines {
  113. _, _, err = sm.SetExpression(line)
  114. if err != nil {
  115. continue
  116. }
  117. }
  118. return sm
  119. }
  120. func WriteEnv(kv []string) error {
  121. defaultGoctlHome, err := pathx.GetDefaultGoctlHome()
  122. if err != nil {
  123. log.Fatalln(err)
  124. }
  125. data := sortedmap.New()
  126. for _, e := range kv {
  127. _, _, err := data.SetExpression(e)
  128. if err != nil {
  129. return err
  130. }
  131. }
  132. data.RangeIf(func(key, value any) bool {
  133. switch key.(string) {
  134. case GoctlHome, GoctlCache:
  135. path := value.(string)
  136. if !pathx.FileExists(path) {
  137. err = fmt.Errorf("[writeEnv]: path %q is not exists", path)
  138. return false
  139. }
  140. }
  141. if goctlEnv.HasKey(key) {
  142. goctlEnv.SetKV(key, value)
  143. return true
  144. } else {
  145. err = fmt.Errorf("[writeEnv]: invalid key: %v", key)
  146. return false
  147. }
  148. })
  149. if err != nil {
  150. return err
  151. }
  152. envFile := filepath.Join(defaultGoctlHome, envFileDir)
  153. return os.WriteFile(envFile, []byte(strings.Join(goctlEnv.Format(), "\n")), 0o777)
  154. }