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