Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject XCADDY_GO_BUILD_FLAGS through go operations (#102) #110

Merged
merged 2 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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