From beb256b0cdba364936c5d9746340d1fcc68bf655 Mon Sep 17 00:00:00 2001 From: Pieter Claerhout Date: Mon, 4 May 2020 08:14:55 +0200 Subject: [PATCH] Added support for building with gotip Fixes #92 --- README.md | 4 +++- go-james.json | 3 ++- internal/builder/builder.go | 21 +++++++++++++++++---- internal/common/logging.go | 5 +++++ internal/config/config.go | 1 + 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 21fbb65..2d772d4 100644 --- a/README.md +++ b/README.md @@ -375,7 +375,8 @@ When you create a new project or init an existing one, a `go-james.json` file wi "ld_flags_linux": [], "extra_args": [ "-trimpath" - ] + ], + "use_gotip": false }, "run": { "environ": { @@ -408,6 +409,7 @@ When you create a new project or init an existing one, a `go-james.json` file wi * `ld_flags_linux`: the linker flags you want to use for building for `linux`. You can find more info about these flags [here](https://golang.org/cmd/link/). * `ld_flags_windows`: the linker flags you want to use for building for `windows`. You can find more info about these flags [here](https://golang.org/cmd/link/). * `extra_args`: contains any extra command-line parameters you want to add to the `go build` command when you run `go-james build`. +* `use_gotip`: setting this to true uses `gotip` to compile instead of the regular `go`command. Make sure you have [`gotip`](https://pkg.go.dev/golang.org/dl/gotip?tab=doc) installed. ### Run Config diff --git a/go-james.json b/go-james.json index 48f7f5b..f44b32f 100644 --- a/go-james.json +++ b/go-james.json @@ -13,7 +13,8 @@ "ld_flags_windows": [], "ld_flags_darwin": [], "ld_flags_linux": [], - "extra_args": ["-trimpath"] + "extra_args": ["-trimpath"], + "use_gotip": false }, "run": { "environ": {} diff --git a/internal/builder/builder.go b/internal/builder/builder.go index ff88b2b..004e8c1 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -6,6 +6,7 @@ import ( "runtime" "strconv" "strings" + "sync" "github.com/kballard/go-shellquote" "github.com/pieterclaerhout/go-james" @@ -14,6 +15,8 @@ import ( "github.com/pkg/errors" ) +var once sync.Once + // Builder implements the "build" command type Builder struct { common.CommandRunner @@ -68,10 +71,10 @@ func (builder Builder) Execute(project common.Project, cfg config.Config) error } if builder.Verbose { - builder.LogInfo("> Compiling for", builder.GOOS+"/"+builder.GOARCH, "using", builder.goVersion()) + builder.LogInfo("> Compiling for", builder.GOOS+"/"+builder.GOARCH, "using", builder.goVersion(cfg)) } - buildCmd := []string{"go", "build"} + buildCmd := []string{builder.goExecuteable(cfg), "build"} if builder.Verbose { buildCmd = append(buildCmd, "-v") @@ -187,9 +190,9 @@ func (builder Builder) ldFlagForVersionInfo(packageName string, name string, val } -func (builder Builder) goVersion() string { +func (builder Builder) goVersion(cfg config.Config) string { - result, err := builder.RunReturnOutput([]string{"go", "version"}, "", map[string]string{}) + result, err := builder.RunReturnOutput([]string{builder.goExecuteable(cfg), "version"}, "", map[string]string{}) if err != nil { builder.LogError("Failed to get Go version:", err) return "" @@ -230,3 +233,13 @@ func (builder Builder) outputPath(cfg config.Config) (string, error) { return outputPath, nil } + +func (builder Builder) goExecuteable(cfg config.Config) string { + if cfg.Build.UseGotip { + once.Do(func() { + builder.LogWarn("> Using gotip to build") + }) + return "gotip" + } + return "go" +} diff --git a/internal/common/logging.go b/internal/common/logging.go index 99f1ca4..0bc2afb 100644 --- a/internal/common/logging.go +++ b/internal/common/logging.go @@ -25,6 +25,11 @@ func (logging Logging) LogInfo(args ...interface{}) { log.Info(args...) } +// LogWarn logs a warning message +func (logging Logging) LogWarn(args ...interface{}) { + log.Warn(args...) +} + // LogError logs an error func (logging Logging) LogError(args ...interface{}) { log.Error(args...) diff --git a/internal/config/config.go b/internal/config/config.go index c56e2bf..a0f83e5 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -39,6 +39,7 @@ type BuildConfig struct { LDFlagsLinux []string `json:"ld_flags_linux"` // The ldflags to pass to the build command for Linux LDFlagsWindows []string `json:"ld_flags_windows"` // The ldflags to pass to the build command for Windows ExtraArgs []string `json:"extra_args"` // The extra arguments to pass to the build command + UseGotip bool `json:"use_gotip"` // Use gotip to build instead of Go itself } // RunConfig contains the run specific configuration settings