mirror of
https://github.com/caddyserver/xcaddy.git
synced 2024-09-27 23:00:16 +01:00
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:
parent
71ce25d1a2
commit
7abc7f5752
6 changed files with 28 additions and 9 deletions
|
@ -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_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_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_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
|
© 2020 Matthew Holt
|
||||||
|
|
|
@ -29,6 +29,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Masterminds/semver/v3"
|
"github.com/Masterminds/semver/v3"
|
||||||
|
"github.com/caddyserver/xcaddy/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Builder can produce a custom Caddy build with the
|
// 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")
|
log.Println("[INFO] Building Caddy")
|
||||||
|
|
||||||
// tidy the module to ensure go.mod and go.sum are consistent with the module prereq
|
// 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 {
|
if err := buildEnv.runCommand(ctx, tidyCmd, b.TimeoutGet); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// compile
|
// compile
|
||||||
cmd := buildEnv.newCommand("go", "build",
|
cmd := buildEnv.newCommand(utils.GetGo(), "build",
|
||||||
"-o", absOutputFile,
|
"-o", absOutputFile,
|
||||||
)
|
)
|
||||||
if b.Debug {
|
if b.Debug {
|
||||||
|
|
|
@ -31,6 +31,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/caddyserver/xcaddy"
|
"github.com/caddyserver/xcaddy"
|
||||||
|
"github.com/caddyserver/xcaddy/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
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
|
// and since this tool is a carry-through for the user's actual
|
||||||
// go.mod, we need to transfer their replace directives through
|
// go.mod, we need to transfer their replace directives through
|
||||||
// to the one we're making
|
// 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
|
cmd.Stderr = os.Stderr
|
||||||
out, err := cmd.Output()
|
out, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -26,6 +26,8 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/caddyserver/xcaddy/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
|
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
|
// initialize the go module
|
||||||
log.Println("[INFO] Initializing 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)
|
err = env.runCommand(ctx, cmd, 10*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -110,7 +112,7 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
|
||||||
replaced := make(map[string]string)
|
replaced := make(map[string]string)
|
||||||
for _, r := range b.Replacements {
|
for _, r := range b.Replacements {
|
||||||
log.Printf("[INFO] Replace %s => %s", r.Old.String(), r.New.String())
|
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()))
|
"-replace", fmt.Sprintf("%s=%s", r.Old.Param(), r.New.Param()))
|
||||||
err := env.runCommand(ctx, cmd, 10*time.Second)
|
err := env.runCommand(ctx, cmd, 10*time.Second)
|
||||||
if err != nil {
|
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.
|
// distinct argument, so we're using an if statement to avoid it.
|
||||||
var cmd *exec.Cmd
|
var cmd *exec.Cmd
|
||||||
if caddy != "" {
|
if caddy != "" {
|
||||||
cmd = env.newCommand("go", "get", "-d", "-v", mod, caddy)
|
cmd = env.newCommand(utils.GetGo(), "get", "-d", "-v", mod, caddy)
|
||||||
} else {
|
} 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)
|
return env.runCommand(ctx, cmd, env.timeoutGoGet)
|
||||||
|
|
13
internal/utils/environment.go
Normal file
13
internal/utils/environment.go
Normal 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
|
||||||
|
}
|
|
@ -3,6 +3,8 @@ package xcaddy
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
|
"github.com/caddyserver/xcaddy/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Compile contains parameters for compilation.
|
// Compile contains parameters for compilation.
|
||||||
|
@ -30,7 +32,7 @@ type Platform struct {
|
||||||
// SupportedPlatforms runs `go tool dist list` to make
|
// SupportedPlatforms runs `go tool dist list` to make
|
||||||
// a list of possible build targets.
|
// a list of possible build targets.
|
||||||
func SupportedPlatforms() ([]Compile, error) {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue