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 <msaa1990@gmail.com>
This commit is contained in:
Peter Magnusson 2022-04-25 18:13:12 +02:00 committed by GitHub
parent 71ce25d1a2
commit 7abc7f5752
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 9 deletions

View file

@ -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.
---
&copy; 2020 Matthew Holt

View file

@ -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 {

View file

@ -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 {

View file

@ -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)

View file

@ -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
}

View file

@ -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
}