Skip to content

Commit

Permalink
Inject XCADDY_GO_BUILD_FLAGS through go operations (#102) (#110)
Browse files Browse the repository at this point in the history
* Extend XCADDY_GO_BUILD_FLAGS usage (#102)

Commit 47f9ded 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.
  • Loading branch information
aabelix authored Aug 16, 2022
1 parent 979de37 commit edc1f41
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
15 changes: 3 additions & 12 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
28 changes: 21 additions & 7 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit edc1f41

Please sign in to comment.