common.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. // This is useful when function returned "sth, err" and avoid many "if err != nil"
  22. // Internal usage only, if user input can cause err, it must be handled
  23. func Must2[T any](v T, err error) T {
  24. Must(err)
  25. return v
  26. }
  27. // Error2 returns the err from the 2nd parameter.
  28. func Error2(v interface{}, err error) error {
  29. return err
  30. }
  31. // envFile returns the name of the Go environment configuration file.
  32. // Copy from https://github.com/golang/go/blob/c4f2a9788a7be04daf931ac54382fbe2cb754938/src/cmd/go/internal/cfg/cfg.go#L150-L166
  33. func envFile() (string, error) {
  34. if file := os.Getenv("GOENV"); file != "" {
  35. if file == "off" {
  36. return "", errors.New("GOENV=off")
  37. }
  38. return file, nil
  39. }
  40. dir, err := os.UserConfigDir()
  41. if err != nil {
  42. return "", err
  43. }
  44. if dir == "" {
  45. return "", errors.New("missing user-config dir")
  46. }
  47. return filepath.Join(dir, "go", "env"), nil
  48. }
  49. // GetRuntimeEnv returns the value of runtime environment variable,
  50. // that is set by running following command: `go env -w key=value`.
  51. func GetRuntimeEnv(key string) (string, error) {
  52. file, err := envFile()
  53. if err != nil {
  54. return "", err
  55. }
  56. if file == "" {
  57. return "", errors.New("missing runtime env file")
  58. }
  59. var data []byte
  60. var runtimeEnv string
  61. data, readErr := os.ReadFile(file)
  62. if readErr != nil {
  63. return "", readErr
  64. }
  65. envStrings := strings.Split(string(data), "\n")
  66. for _, envItem := range envStrings {
  67. envItem = strings.TrimSuffix(envItem, "\r")
  68. envKeyValue := strings.Split(envItem, "=")
  69. if strings.EqualFold(strings.TrimSpace(envKeyValue[0]), key) {
  70. runtimeEnv = strings.TrimSpace(envKeyValue[1])
  71. }
  72. }
  73. return runtimeEnv, nil
  74. }
  75. // GetGOBIN returns GOBIN environment variable as a string. It will NOT be empty.
  76. func GetGOBIN() string {
  77. // The one set by user explicitly by `export GOBIN=/path` or `env GOBIN=/path command`
  78. GOBIN := os.Getenv("GOBIN")
  79. if GOBIN == "" {
  80. var err error
  81. // The one set by user by running `go env -w GOBIN=/path`
  82. GOBIN, err = GetRuntimeEnv("GOBIN")
  83. if err != nil {
  84. // The default one that Golang uses
  85. return filepath.Join(build.Default.GOPATH, "bin")
  86. }
  87. if GOBIN == "" {
  88. return filepath.Join(build.Default.GOPATH, "bin")
  89. }
  90. return GOBIN
  91. }
  92. return GOBIN
  93. }
  94. // GetGOPATH returns GOPATH environment variable as a string. It will NOT be empty.
  95. func GetGOPATH() string {
  96. // The one set by user explicitly by `export GOPATH=/path` or `env GOPATH=/path command`
  97. GOPATH := os.Getenv("GOPATH")
  98. if GOPATH == "" {
  99. var err error
  100. // The one set by user by running `go env -w GOPATH=/path`
  101. GOPATH, err = GetRuntimeEnv("GOPATH")
  102. if err != nil {
  103. // The default one that Golang uses
  104. return build.Default.GOPATH
  105. }
  106. if GOPATH == "" {
  107. return build.Default.GOPATH
  108. }
  109. return GOPATH
  110. }
  111. return GOPATH
  112. }
  113. // GetModuleName returns the value of module in `go.mod` file.
  114. func GetModuleName(pathToProjectRoot string) (string, error) {
  115. var moduleName string
  116. loopPath := pathToProjectRoot
  117. for {
  118. if idx := strings.LastIndex(loopPath, string(filepath.Separator)); idx >= 0 {
  119. gomodPath := filepath.Join(loopPath, "go.mod")
  120. gomodBytes, err := os.ReadFile(gomodPath)
  121. if err != nil {
  122. loopPath = loopPath[:idx]
  123. continue
  124. }
  125. gomodContent := string(gomodBytes)
  126. moduleIdx := strings.Index(gomodContent, "module ")
  127. newLineIdx := strings.Index(gomodContent, "\n")
  128. if moduleIdx >= 0 {
  129. if newLineIdx >= 0 {
  130. moduleName = strings.TrimSpace(gomodContent[moduleIdx+6 : newLineIdx])
  131. moduleName = strings.TrimSuffix(moduleName, "\r")
  132. } else {
  133. moduleName = strings.TrimSpace(gomodContent[moduleIdx+6:])
  134. }
  135. return moduleName, nil
  136. }
  137. return "", fmt.Errorf("can not get module path in `%s`", gomodPath)
  138. }
  139. break
  140. }
  141. return moduleName, fmt.Errorf("no `go.mod` file in every parent directory of `%s`", pathToProjectRoot)
  142. }