Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gocmd.Name: use env GOP_GOCMD #1550

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}

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