Skip to content

Commit

Permalink
Merge pull request #1550 from xushiwei/clean
Browse files Browse the repository at this point in the history
gocmd.Name: use env GOP_GOCMD
  • Loading branch information
xushiwei authored Nov 20, 2023
2 parents fefc719 + b80a6c1 commit a9ccf0a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cmd/internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
Expand Down
20 changes: 19 additions & 1 deletion x/gocmd/gocmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type GopEnv = env.Gop

type Config struct {
Gop *GopEnv
GoCmd string
Flags []string
Run func(cmd *exec.Cmd) error
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}

// -----------------------------------------------------------------------------

0 comments on commit a9ccf0a

Please sign in to comment.