From 79fef6b7bb6eac3f29ef4649e06549b4cbe2a70a Mon Sep 17 00:00:00 2001 From: Aapo Keskimolo Date: Thu, 16 Jun 2022 11:38:09 +0300 Subject: [PATCH] Enable overriding build flags from environment Add environment variable XCADDY_GO_BUILD_FLAGS to override default build arguments from environment (#102). To support flags with variable arguments Unix-style quoting is supported. --- README.md | 1 + builder.go | 19 +++++++++++++++---- cmd/main.go | 2 ++ environment.go | 2 ++ go.mod | 5 ++++- go.sum | 2 ++ 6 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 69750d8..7fcecb5 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,7 @@ Because the subcommands and flags are constrained to benefit rapid plugin protot - `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_WHICH_GO` sets the go command to use when for example more then 1 version of go is installed. +- `XCADDY_GO_BUILD_FLAGS` overrides default build arguments. Supports Unix-style shell quoting, for example: XCADDY_GO_BUILD_FLAGS="-ldflags '-w s'". --- © 2020 Matthew Holt diff --git a/builder.go b/builder.go index 877b1d2..6df0b3e 100644 --- a/builder.go +++ b/builder.go @@ -30,6 +30,7 @@ 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 @@ -45,6 +46,7 @@ type Builder struct { SkipCleanup bool `json:"skip_cleanup,omitempty"` SkipBuild bool `json:"skip_build,omitempty"` Debug bool `json:"debug,omitempty"` + BuildFlags string `json:"build_flags,omitempty"` } // Build builds Caddy at the configured version with the @@ -115,10 +117,19 @@ func (b Builder) Build(ctx context.Context, outputFile string) error { // support dlv cmd.Args = append(cmd.Args, "-gcflags", "all=-N -l") } else { - cmd.Args = append(cmd.Args, - "-ldflags", "-w -s", // trim debug symbols - "-trimpath", - ) + 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 { + cmd.Args = append(cmd.Args, + "-ldflags", "-w -s", // trim debug symbols + "-trimpath", + ) + } } if b.RaceDetector { diff --git a/cmd/main.go b/cmd/main.go index 61c9615..e9a26cb 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -40,6 +40,7 @@ var ( skipBuild = os.Getenv("XCADDY_SKIP_BUILD") == "1" skipCleanup = os.Getenv("XCADDY_SKIP_CLEANUP") == "1" || skipBuild buildDebugOutput = os.Getenv("XCADDY_DEBUG") == "1" + buildFlags = os.Getenv("XCADDY_GO_BUILD_FLAGS") ) func Main() { @@ -134,6 +135,7 @@ func runBuild(ctx context.Context, args []string) error { SkipBuild: skipBuild, SkipCleanup: skipCleanup, Debug: buildDebugOutput, + BuildFlags: buildFlags, } err := builder.Build(ctx, output) if err != nil { diff --git a/environment.go b/environment.go index 839c113..c79256f 100644 --- a/environment.go +++ b/environment.go @@ -98,6 +98,7 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) { tempFolder: tempFolder, timeoutGoGet: b.TimeoutGet, skipCleanup: b.SkipCleanup, + buildFlags: b.BuildFlags, } // initialize the go module @@ -177,6 +178,7 @@ type environment struct { tempFolder string timeoutGoGet time.Duration skipCleanup bool + buildFlags string } // Close cleans up the build environment, including deleting diff --git a/go.mod b/go.mod index 4e48de9..0fc9b6a 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/caddyserver/xcaddy go 1.14 -require github.com/Masterminds/semver/v3 v3.1.1 +require ( + github.com/Masterminds/semver/v3 v3.1.1 + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 +) diff --git a/go.sum b/go.sum index 471bde9..65f1f6f 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc= github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=