diff --git a/cmd/internal/env/env.go b/cmd/internal/env/env.go index 0fcb86a72..43febb859 100644 --- a/cmd/internal/env/env.go +++ b/cmd/internal/env/env.go @@ -27,6 +27,7 @@ import ( "github.com/goplus/gop/cmd/internal/base" "github.com/goplus/gop/env" + "github.com/goplus/gop/x/gocmd" "github.com/goplus/mod" "github.com/goplus/mod/modcache" ) @@ -71,6 +72,7 @@ func runCmd(_ *base.Command, args []string) { gopEnv["BUILDDATE"] = env.BuildDate() gopEnv["GOPVERSION"] = env.Version() gopEnv["GOPROOT"] = env.GOPROOT() + gopEnv["GOP_GOCMD"] = gocmd.Name() gopEnv["GOMODCACHE"] = modcache.GOMODCACHE gopEnv["GOPMOD"], _ = mod.GOPMOD("", 0) gopEnv["HOME"] = env.HOME() diff --git a/x/gocmd/gocmd.go b/x/gocmd/gocmd.go index ffb894666..30a8b14ed 100644 --- a/x/gocmd/gocmd.go +++ b/x/gocmd/gocmd.go @@ -29,6 +29,7 @@ type GopEnv = env.Gop type Config struct { Gop *GopEnv + GoCmd string Flags []string Run func(cmd *exec.Cmd) error } @@ -39,12 +40,16 @@ func doWithArgs(op string, conf *Config, args ...string) (err error) { if conf == nil { conf = new(Config) } + goCmd := conf.GoCmd + if goCmd == "" { + goCmd = Name() + } exargs := make([]string, 1, 16) exargs[0] = op exargs = appendLdflags(exargs, conf.Gop) exargs = append(exargs, conf.Flags...) exargs = append(exargs, args...) - cmd := exec.Command("go", exargs...) + cmd := exec.Command(goCmd, exargs...) run := conf.Run if run == nil { run = runCmd @@ -83,3 +88,16 @@ func appendLdflags(exargs []string, env *GopEnv) []string { } // ----------------------------------------------------------------------------- + +// Name returns name of the go command. +// It returns value of environment variable `GOP_GOCMD` if not empty. +// If not found, it returns `go`. +func Name() string { + goCmd := os.Getenv("GOP_GOCMD") + if goCmd == "" { + goCmd = "go" + } + return goCmd +} + +// -----------------------------------------------------------------------------