Skip to content

Commit

Permalink
fix variable environment extraction (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
WeidiDeng authored May 10, 2024
1 parent bff6878 commit 639d0b1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
5 changes: 3 additions & 2 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"time"

"github.com/Masterminds/semver/v3"
"github.com/caddyserver/xcaddy/internal/utils"
)

// Builder can produce a custom Caddy build with the
Expand Down Expand Up @@ -76,10 +77,10 @@ func (b Builder) Build(ctx context.Context, outputFile string) error {

// set some defaults from the environment, if applicable
if b.OS == "" {
b.OS = os.Getenv("GOOS")
b.OS = utils.GetGOOS()
}
if b.Arch == "" {
b.Arch = os.Getenv("GOARCH")
b.Arch = utils.GetGOARCH()
}
if b.ARM == "" {
b.ARM = os.Getenv("GOARM")
Expand Down
9 changes: 7 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,19 @@ func runBuild(ctx context.Context, args []string) error {
log.Fatalf("[FATAL] %v", err)
}

// done if we're skipping the build
if builder.SkipBuild {
return nil
}

// if requested, run setcap to allow binding to low ports
err = setcapIfRequested(output)
if err != nil {
return err
}

// prove the build is working by printing the version
if runtime.GOOS == os.Getenv("GOOS") && runtime.GOARCH == os.Getenv("GOARCH") {
if runtime.GOOS == utils.GetGOOS() && runtime.GOARCH == utils.GetGOARCH() {
if !filepath.IsAbs(output) {
output = "." + string(filepath.Separator) + output
}
Expand All @@ -195,7 +200,7 @@ func runBuild(ctx context.Context, args []string) error {
func getCaddyOutputFile() string {
f := "." + string(filepath.Separator) + "caddy"
// compiling for Windows or compiling on windows without setting GOOS, use .exe extension
if os.Getenv("GOOS") == "windows" || (os.Getenv("GOOS") == "" && runtime.GOOS == "windows") {
if utils.GetGOOS() == "windows" {
f += ".exe"
}
return f
Expand Down
23 changes: 22 additions & 1 deletion internal/utils/environment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package utils

import "os"
import (
"os"
"runtime"
)

// GetGo returns the go executable to use depending on what
// is set in the XCADDY_WHICH_GO environment variable.
Expand All @@ -11,3 +14,21 @@ func GetGo() string {
}
return g
}

// GetGOOS returns the compilation target OS
func GetGOOS() string {
o := os.Getenv("GOOS")
if o == "" {
return runtime.GOOS
}
return o
}

// GetGOARCH returns the compilation target architecture
func GetGOARCH() string {
a := os.Getenv("GOARCH")
if a == "" {
return runtime.GOARCH
}
return a
}

0 comments on commit 639d0b1

Please sign in to comment.