env.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package env
  2. import (
  3. "os"
  4. "os/exec"
  5. "path/filepath"
  6. "runtime"
  7. "strings"
  8. "time"
  9. "github.com/zeromicro/go-zero/tools/goctl/vars"
  10. )
  11. const (
  12. bin = "bin"
  13. binGo = "go"
  14. binProtoc = "protoc"
  15. binProtocGenGo = "protoc-gen-go"
  16. binProtocGenGrpcGo = "protoc-gen-go-grpc"
  17. cstOffset = 60 * 60 * 8 // 8 hours offset for Chinese Standard Time
  18. )
  19. // InChina returns whether the current time is in China Standard Time.
  20. func InChina() bool {
  21. _, offset := time.Now().Zone()
  22. return offset == cstOffset
  23. }
  24. // LookUpGo searches an executable go in the directories
  25. // named by the GOROOT/bin or PATH environment variable.
  26. func LookUpGo() (string, error) {
  27. goRoot := runtime.GOROOT()
  28. suffix := getExeSuffix()
  29. xGo := binGo + suffix
  30. path := filepath.Join(goRoot, bin, xGo)
  31. if _, err := os.Stat(path); err == nil {
  32. return path, nil
  33. }
  34. return LookPath(xGo)
  35. }
  36. // LookUpProtoc searches an executable protoc in the directories
  37. // named by the PATH environment variable.
  38. func LookUpProtoc() (string, error) {
  39. suffix := getExeSuffix()
  40. xProtoc := binProtoc + suffix
  41. return LookPath(xProtoc)
  42. }
  43. // LookUpProtocGenGo searches an executable protoc-gen-go in the directories
  44. // named by the PATH environment variable.
  45. func LookUpProtocGenGo() (string, error) {
  46. suffix := getExeSuffix()
  47. xProtocGenGo := binProtocGenGo + suffix
  48. return LookPath(xProtocGenGo)
  49. }
  50. // LookUpProtocGenGoGrpc searches an executable protoc-gen-go-grpc in the directories
  51. // named by the PATH environment variable.
  52. func LookUpProtocGenGoGrpc() (string, error) {
  53. suffix := getExeSuffix()
  54. xProtocGenGoGrpc := binProtocGenGrpcGo + suffix
  55. return LookPath(xProtocGenGoGrpc)
  56. }
  57. // LookPath searches for an executable named file in the
  58. // directories named by the PATH environment variable,
  59. // for the os windows, the named file will be spliced with the
  60. // .exe suffix.
  61. func LookPath(xBin string) (string, error) {
  62. suffix := getExeSuffix()
  63. if len(suffix) > 0 && !strings.HasSuffix(xBin, suffix) {
  64. xBin = xBin + suffix
  65. }
  66. bin, err := exec.LookPath(xBin)
  67. if err != nil {
  68. return "", err
  69. }
  70. return bin, nil
  71. }
  72. // CanExec reports whether the current system can start new processes
  73. // using os.StartProcess or (more commonly) exec.Command.
  74. func CanExec() bool {
  75. switch runtime.GOOS {
  76. case vars.OsJs, vars.OsIOS:
  77. return false
  78. default:
  79. return true
  80. }
  81. }
  82. func getExeSuffix() string {
  83. if runtime.GOOS == vars.OsWindows {
  84. return ".exe"
  85. }
  86. return ""
  87. }