Skip to content

Commit

Permalink
Do not use only hardcoded "go" (#97)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
kmpm and mohammed90 authored Apr 25, 2022
1 parent 71ce25d commit 7abc7f5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"strings"

"github.com/caddyserver/xcaddy"
"github.com/caddyserver/xcaddy/internal/utils"
)

var (
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"path/filepath"
"strings"
"time"

"github.com/caddyserver/xcaddy/internal/utils"
)

func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions internal/utils/environment.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 3 additions & 1 deletion platforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package xcaddy
import (
"encoding/json"
"os/exec"

"github.com/caddyserver/xcaddy/internal/utils"
)

// Compile contains parameters for compilation.
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 7abc7f5

Please sign in to comment.