From 7abc7f575264681008ae458e41c29c99c6c917c1 Mon Sep 17 00:00:00 2001 From: Peter Magnusson Date: Mon, 25 Apr 2022 18:13:12 +0200 Subject: [PATCH] Do not use only hardcoded "go" (#97) * fix: do not use only hardcoded "go" * fixed comments and added env to readme * move GetGo to an internal package Co-authored-by: Mohammed Al Sahaf --- README.md | 2 +- builder.go | 5 +++-- cmd/main.go | 3 ++- environment.go | 10 ++++++---- internal/utils/environment.go | 13 +++++++++++++ platforms.go | 4 +++- 6 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 internal/utils/environment.go diff --git a/README.md b/README.md index 028affc..c8e12f5 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ Because the subcommands and flags are constrained to benefit rapid plugin protot - `XCADDY_SETCAP=1` will run `sudo setcap cap_net_bind_service=+ep` on the temporary binary before running it when in dev mode. - `XCADDY_SKIP_BUILD=1` causes xcaddy to not compile the program, it is used in conjunction with build tools such as [GoReleaser](https://goreleaser.com). Implies `XCADDY_SKIP_CLEANUP=1`. - `XCADDY_SKIP_CLEANUP=1` causes xcaddy to leave build artifacts on disk after exiting. - +- `XCADDY_WHICH_GO` sets the go command to use when for example more then 1 version of go is installed. --- © 2020 Matthew Holt diff --git a/builder.go b/builder.go index 51c14b6..877b1d2 100644 --- a/builder.go +++ b/builder.go @@ -29,6 +29,7 @@ import ( "time" "github.com/Masterminds/semver/v3" + "github.com/caddyserver/xcaddy/internal/utils" ) // Builder can produce a custom Caddy build with the @@ -101,13 +102,13 @@ func (b Builder) Build(ctx context.Context, outputFile string) error { log.Println("[INFO] Building Caddy") // tidy the module to ensure go.mod and go.sum are consistent with the module prereq - tidyCmd := buildEnv.newCommand("go", "mod", "tidy") + tidyCmd := buildEnv.newCommand(utils.GetGo(), "mod", "tidy") if err := buildEnv.runCommand(ctx, tidyCmd, b.TimeoutGet); err != nil { return err } // compile - cmd := buildEnv.newCommand("go", "build", + cmd := buildEnv.newCommand(utils.GetGo(), "build", "-o", absOutputFile, ) if b.Debug { diff --git a/cmd/main.go b/cmd/main.go index 96dd941..61c9615 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -31,6 +31,7 @@ import ( "strings" "github.com/caddyserver/xcaddy" + "github.com/caddyserver/xcaddy/internal/utils" ) var ( @@ -177,7 +178,7 @@ func runDev(ctx context.Context, args []string) error { // and since this tool is a carry-through for the user's actual // go.mod, we need to transfer their replace directives through // to the one we're making - cmd := exec.Command("go", "list", "-m", "-json", "all") + cmd := exec.Command(utils.GetGo(), "list", "-m", "-json", "all") cmd.Stderr = os.Stderr out, err := cmd.Output() if err != nil { diff --git a/environment.go b/environment.go index 387db2c..fabb731 100644 --- a/environment.go +++ b/environment.go @@ -26,6 +26,8 @@ import ( "path/filepath" "strings" "time" + + "github.com/caddyserver/xcaddy/internal/utils" ) func (b Builder) newEnvironment(ctx context.Context) (*environment, error) { @@ -100,7 +102,7 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) { // initialize the go module log.Println("[INFO] Initializing Go module") - cmd := env.newCommand("go", "mod", "init", "caddy") + cmd := env.newCommand(utils.GetGo(), "mod", "init", "caddy") err = env.runCommand(ctx, cmd, 10*time.Second) if err != nil { return nil, err @@ -110,7 +112,7 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) { replaced := make(map[string]string) for _, r := range b.Replacements { log.Printf("[INFO] Replace %s => %s", r.Old.String(), r.New.String()) - cmd := env.newCommand("go", "mod", "edit", + cmd := env.newCommand(utils.GetGo(), "mod", "edit", "-replace", fmt.Sprintf("%s=%s", r.Old.Param(), r.New.Param())) err := env.runCommand(ctx, cmd, 10*time.Second) if err != nil { @@ -263,9 +265,9 @@ func (env environment) execGoGet(ctx context.Context, modulePath, moduleVersion, // distinct argument, so we're using an if statement to avoid it. var cmd *exec.Cmd if caddy != "" { - cmd = env.newCommand("go", "get", "-d", "-v", mod, caddy) + cmd = env.newCommand(utils.GetGo(), "get", "-d", "-v", mod, caddy) } else { - cmd = env.newCommand("go", "get", "-d", "-v", mod) + cmd = env.newCommand(utils.GetGo(), "get", "-d", "-v", mod) } return env.runCommand(ctx, cmd, env.timeoutGoGet) diff --git a/internal/utils/environment.go b/internal/utils/environment.go new file mode 100644 index 0000000..028eebb --- /dev/null +++ b/internal/utils/environment.go @@ -0,0 +1,13 @@ +package utils + +import "os" + +// GetGo returns the go executable to use depending on what +// is set in the XCADDY_WHICH_GO environment variable. +func GetGo() string { + g := os.Getenv("XCADDY_WHICH_GO") + if g == "" { + return "go" + } + return g +} diff --git a/platforms.go b/platforms.go index b336fa8..b3ec6df 100644 --- a/platforms.go +++ b/platforms.go @@ -3,6 +3,8 @@ package xcaddy import ( "encoding/json" "os/exec" + + "github.com/caddyserver/xcaddy/internal/utils" ) // Compile contains parameters for compilation. @@ -30,7 +32,7 @@ type Platform struct { // SupportedPlatforms runs `go tool dist list` to make // a list of possible build targets. func SupportedPlatforms() ([]Compile, error) { - out, err := exec.Command("go", "tool", "dist", "list", "-json").Output() + out, err := exec.Command(utils.GetGo(), "tool", "dist", "list", "-json").Output() if err != nil { return nil, err }