common.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Package common contains common utilities that are shared among other packages.
  2. // See each sub-package for detail.
  3. package common
  4. import (
  5. "fmt"
  6. "go/build"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "github.com/xtls/xray-core/common/errors"
  11. )
  12. // ErrNoClue is for the situation that existing information is not enough to make a decision. For example, Router may return this error when there is no suitable route.
  13. var ErrNoClue = errors.New("not enough information for making a decision")
  14. // Must panics if err is not nil.
  15. func Must(err error) {
  16. if err != nil {
  17. panic(err)
  18. }
  19. }
  20. // Must2 panics if the second parameter is not nil, otherwise returns the first parameter.
  21. func Must2(v interface{}, err error) interface{} {
  22. Must(err)
  23. return v
  24. }
  25. // Error2 returns the err from the 2nd parameter.
  26. func Error2(v interface{}, err error) error {
  27. return err
  28. }
  29. // envFile returns the name of the Go environment configuration file.
  30. // Copy from https://github.com/golang/go/blob/c4f2a9788a7be04daf931ac54382fbe2cb754938/src/cmd/go/internal/cfg/cfg.go#L150-L166
  31. func envFile() (string, error) {
  32. if file := os.Getenv("GOENV"); file != "" {
  33. if file == "off" {
  34. return "", errors.New("GOENV=off")
  35. }
  36. return file, nil
  37. }
  38. dir, err := os.UserConfigDir()
  39. if err != nil {
  40. return "", err
  41. }
  42. if dir == "" {
  43. return "", errors.New("missing user-config dir")
  44. }
  45. return filepath.Join(dir, "go", "env"), nil
  46. }
  47. // GetRuntimeEnv returns the value of runtime environment variable,
  48. // that is set by running following command: `go env -w key=value`.
  49. func GetRuntimeEnv(key string) (string, error) {
  50. file, err := envFile()
  51. if err != nil {
  52. return "", err
  53. }
  54. if file == "" {
  55. return "", errors.New("missing runtime env file")
  56. }
  57. var data []byte
  58. var runtimeEnv string
  59. data, readErr := os.ReadFile(file)
  60. if readErr != nil {
  61. return "", readErr
  62. }
  63. envStrings := strings.Split(string(data), "\n")
  64. for _, envItem := range envStrings {
  65. envItem = strings.TrimSuffix(envItem, "\r")
  66. envKeyValue := strings.Split(envItem, "=")
  67. if strings.EqualFold(strings.TrimSpace(envKeyValue[0]), key) {
  68. runtimeEnv = strings.TrimSpace(envKeyValue[1])
  69. }
  70. }
  71. return runtimeEnv, nil
  72. }
  73. // GetGOBIN returns GOBIN environment variable as a string. It will NOT be empty.
  74. func GetGOBIN() string {
  75. // The one set by user explicitly by `export GOBIN=/path` or `env GOBIN=/path command`
  76. GOBIN := os.Getenv("GOBIN")
  77. if GOBIN == "" {
  78. var err error
  79. // The one set by user by running `go env -w GOBIN=/path`
  80. GOBIN, err = GetRuntimeEnv("GOBIN")
  81. if err != nil {
  82. // The default one that Golang uses
  83. return filepath.Join(build.Default.GOPATH, "bin")
  84. }
  85. if GOBIN == "" {
  86. return filepath.Join(build.Default.GOPATH, "bin")
  87. }
  88. return GOBIN
  89. }
  90. return GOBIN
  91. }
  92. // GetGOPATH returns GOPATH environment variable as a string. It will NOT be empty.
  93. func GetGOPATH() string {
  94. // The one set by user explicitly by `export GOPATH=/path` or `env GOPATH=/path command`
  95. GOPATH := os.Getenv("GOPATH")
  96. if GOPATH == "" {
  97. var err error
  98. // The one set by user by running `go env -w GOPATH=/path`
  99. GOPATH, err = GetRuntimeEnv("GOPATH")
  100. if err != nil {
  101. // The default one that Golang uses
  102. return build.Default.GOPATH
  103. }
  104. if GOPATH == "" {
  105. return build.Default.GOPATH
  106. }
  107. return GOPATH
  108. }
  109. return GOPATH
  110. }
  111. // GetModuleName returns the value of module in `go.mod` file.
  112. func GetModuleName(pathToProjectRoot string) (string, error) {
  113. var moduleName string
  114. loopPath := pathToProjectRoot
  115. for {
  116. if idx := strings.LastIndex(loopPath, string(filepath.Separator)); idx >= 0 {
  117. gomodPath := filepath.Join(loopPath, "go.mod")
  118. gomodBytes, err := os.ReadFile(gomodPath)
  119. if err != nil {
  120. loopPath = loopPath[:idx]
  121. continue
  122. }
  123. gomodContent := string(gomodBytes)
  124. moduleIdx := strings.Index(gomodContent, "module ")
  125. newLineIdx := strings.Index(gomodContent, "\n")
  126. if moduleIdx >= 0 {
  127. if newLineIdx >= 0 {
  128. moduleName = strings.TrimSpace(gomodContent[moduleIdx+6 : newLineIdx])
  129. moduleName = strings.TrimSuffix(moduleName, "\r")
  130. } else {
  131. moduleName = strings.TrimSpace(gomodContent[moduleIdx+6:])
  132. }
  133. return moduleName, nil
  134. }
  135. return "", fmt.Errorf("can not get module path in `%s`", gomodPath)
  136. }
  137. break
  138. }
  139. return moduleName, fmt.Errorf("no `go.mod` file in every parent directory of `%s`", pathToProjectRoot)
  140. }