Add XCADDY_GO_BUILD_FLAGS env var (#104)

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.
This commit is contained in:
Aapo Keskimölö 2022-06-20 22:20:05 +03:00 committed by GitHub
parent a83fac18bf
commit 47f9ded5d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 5 deletions

View file

@ -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

View file

@ -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 {

View file

@ -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 {

View file

@ -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

5
go.mod
View file

@ -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
)

2
go.sum
View file

@ -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=