mirror of
https://github.com/caddyserver/xcaddy.git
synced 2025-03-09 23:29:06 +01:00
* Extend XCADDY_GO_BUILD_FLAGS usage (#102)
Commit 47f9ded5d8
is not sufficient alone to
ensure that all go modules are installed with write bit set because 'go mod'
or 'go get' might install modules as read-only, too.
If set, the environment variable is appended for the other go commands that
support build flags.
* Make running 'go' implicit for build environment
The method newCommand runs only go commands so let's make command 'go'
implicit and rename it to make it more verbose.
This commit is contained in:
parent
979de371dd
commit
edc1f41778
2 changed files with 24 additions and 19 deletions
15
builder.go
15
builder.go
|
@ -29,8 +29,6 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/caddyserver/xcaddy/internal/utils"
|
||||
"github.com/google/shlex"
|
||||
)
|
||||
|
||||
// Builder can produce a custom Caddy build with the
|
||||
|
@ -104,27 +102,20 @@ 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(utils.GetGo(), "mod", "tidy")
|
||||
tidyCmd := buildEnv.newGoCommand("mod", "tidy")
|
||||
if err := buildEnv.runCommand(ctx, tidyCmd, b.TimeoutGet); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// compile
|
||||
cmd := buildEnv.newCommand(utils.GetGo(), "build",
|
||||
cmd := buildEnv.newGoCommand("build",
|
||||
"-o", absOutputFile,
|
||||
)
|
||||
if b.Debug {
|
||||
// support dlv
|
||||
cmd.Args = append(cmd.Args, "-gcflags", "all=-N -l")
|
||||
} else {
|
||||
if b.BuildFlags != "" {
|
||||
// override build flags from environment if given
|
||||
flags, err := shlex.Split(b.BuildFlags)
|
||||
if err != nil {
|
||||
log.Fatalf("[FATAL] Splitting arguments failed: %s", b.BuildFlags)
|
||||
}
|
||||
cmd.Args = append(cmd.Args, flags...)
|
||||
} else {
|
||||
if buildEnv.buildFlags == "" {
|
||||
cmd.Args = append(cmd.Args,
|
||||
"-ldflags", "-w -s", // trim debug symbols
|
||||
"-trimpath",
|
||||
|
|
|
@ -28,6 +28,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/caddyserver/xcaddy/internal/utils"
|
||||
"github.com/google/shlex"
|
||||
)
|
||||
|
||||
func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
|
||||
|
@ -103,7 +104,8 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
|
|||
|
||||
// initialize the go module
|
||||
log.Println("[INFO] Initializing Go module")
|
||||
cmd := env.newCommand(utils.GetGo(), "mod", "init", "caddy")
|
||||
cmd := env.newGoCommand("mod", "init")
|
||||
cmd.Args = append(cmd.Args, "caddy")
|
||||
err = env.runCommand(ctx, cmd, 10*time.Second)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -113,7 +115,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(utils.GetGo(), "mod", "edit",
|
||||
cmd := env.newGoCommand("mod", "edit",
|
||||
"-replace", fmt.Sprintf("%s=%s", r.Old.Param(), r.New.Param()))
|
||||
err := env.runCommand(ctx, cmd, 10*time.Second)
|
||||
if err != nil {
|
||||
|
@ -192,11 +194,23 @@ func (env environment) Close() error {
|
|||
return os.RemoveAll(env.tempFolder)
|
||||
}
|
||||
|
||||
func (env environment) newCommand(command string, args ...string) *exec.Cmd {
|
||||
cmd := exec.Command(command, args...)
|
||||
func (env environment) newGoCommand(args ...string) *exec.Cmd {
|
||||
cmd := exec.Command(utils.GetGo(), args...)
|
||||
cmd.Dir = env.tempFolder
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
if env.buildFlags == "" {
|
||||
return cmd
|
||||
}
|
||||
|
||||
flags, err := shlex.Split(env.buildFlags)
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] Splitting arguments failed: %s", env.buildFlags)
|
||||
return cmd
|
||||
}
|
||||
cmd.Args = append(cmd.Args, flags...)
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
|
@ -262,14 +276,14 @@ func (env environment) execGoGet(ctx context.Context, modulePath, moduleVersion,
|
|||
caddy += "@" + caddyVersion
|
||||
}
|
||||
|
||||
cmd := env.newGoCommand("get", "-d", "-v")
|
||||
// using an empty string as an additional argument to "go get"
|
||||
// breaks the command since it treats the empty string as a
|
||||
// distinct argument, so we're using an if statement to avoid it.
|
||||
var cmd *exec.Cmd
|
||||
if caddy != "" {
|
||||
cmd = env.newCommand(utils.GetGo(), "get", "-d", "-v", mod, caddy)
|
||||
cmd.Args = append(cmd.Args, mod, caddy)
|
||||
} else {
|
||||
cmd = env.newCommand(utils.GetGo(), "get", "-d", "-v", mod)
|
||||
cmd.Args = append(cmd.Args, mod)
|
||||
}
|
||||
|
||||
return env.runCommand(ctx, cmd, env.timeoutGoGet)
|
||||
|
|
Loading…
Reference in a new issue