-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
44 lines (36 loc) · 916 Bytes
/
run.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
package quickstart
import (
"os"
"os/exec"
"runtime"
"github.com/jageros/goctl/util/env"
"github.com/jageros/goctl/vars"
)
const goproxy = "GOPROXY=https://goproxy.cn,direct"
func goStart(dir string) {
execCommand(dir, "go run .", prepareGoProxyEnv()...)
}
func goModTidy(dir string) int {
log.Debug(">> go mod tidy")
return execCommand(dir, "go mod tidy", prepareGoProxyEnv()...)
}
func execCommand(dir, arg string, envArgs ...string) int {
cmd := exec.Command("sh", "-c", arg)
if runtime.GOOS == vars.OsWindows {
cmd = exec.Command("cmd.exe", "/c", arg)
}
env := append([]string(nil), os.Environ()...)
env = append(env, envArgs...)
cmd.Env = env
cmd.Dir = dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
_ = cmd.Run()
return cmd.ProcessState.ExitCode()
}
func prepareGoProxyEnv(envArgs ...string) []string {
if env.InChina() {
return append(envArgs, goproxy)
}
return envArgs
}