Skip to content

Commit

Permalink
Extend XCADDY_GO_BUILD_FLAGS usage (#102)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aabelix committed Aug 15, 2022
1 parent 47f9ded commit deb12e9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
14 changes: 6 additions & 8 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (

"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 @@ -105,6 +104,10 @@ func (b Builder) Build(ctx context.Context, outputFile string) error {

// tidy the module to ensure go.mod and go.sum are consistent with the module prereq
tidyCmd := buildEnv.newCommand(utils.GetGo(), "mod", "tidy")
buildFlags := buildEnv.parseBuildFlags()
if len(buildFlags) > 0 {
tidyCmd.Args = append(tidyCmd.Args, buildFlags...)
}
if err := buildEnv.runCommand(ctx, tidyCmd, b.TimeoutGet); err != nil {
return err
}
Expand All @@ -117,13 +120,8 @@ func (b Builder) Build(ctx context.Context, outputFile string) error {
// 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...)
if len(buildFlags) > 0 {
cmd.Args = append(cmd.Args, buildFlags...)
} else {
cmd.Args = append(cmd.Args,
"-ldflags", "-w -s", // trim debug symbols
Expand Down
31 changes: 29 additions & 2 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 @@ -266,15 +267,41 @@ func (env environment) execGoGet(ctx context.Context, modulePath, moduleVersion,
// 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
args := []string{"get", "-d", "-v"}
buildFlags := env.parseBuildFlags()
if len(buildFlags) > 0 {
args = append(args, buildFlags...)
}
if caddy != "" {
cmd = env.newCommand(utils.GetGo(), "get", "-d", "-v", mod, caddy)
args = append(args, mod, caddy)
} else {
cmd = env.newCommand(utils.GetGo(), "get", "-d", "-v", mod)
args = append(args, mod)
}
cmd = env.newCommand(utils.GetGo(), args...)

return env.runCommand(ctx, cmd, env.timeoutGoGet)
}

// parseBuildFlags splits buildFlags to array that can be appended to a go
// command that supports build flags. If parsing fails or value is empty, empty
// list will be returned.
func (env environment) parseBuildFlags() []string {
var v []string

if env.buildFlags == "" {
return v
}

flags, err := shlex.Split(env.buildFlags)
if err != nil {
log.Printf("[ERROR] Splitting arguments failed: %s", env.buildFlags)
return v
}
v = append(v, flags...)

return v
}

type goModTemplateContext struct {
CaddyModule string
Plugins []string
Expand Down

0 comments on commit deb12e9

Please sign in to comment.