From 53b2cebb66083ba837fafc931a5699e61ac2149b Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Fri, 23 Apr 2021 17:33:13 -0300 Subject: [PATCH] Updated the version output to use Go module build information if avalable. Enabled GoReleaser module proxying for verifiable builds. Co-authored-by: Jamie Edge Co-authored-by: Carlos Alexandro Becker --- .goreleaser.yml | 7 ++++++- CHANGELOG.md | 5 +++++ cmd/task/task.go | 23 +++++++++++++++++++++-- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index e67d7b9f..fa776cef 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,6 +1,6 @@ build: binary: task - main: cmd/task/task.go + main: cmd/task goos: - windows - darwin @@ -17,6 +17,11 @@ build: goarch: 386 env: - CGO_ENABLED=0 + ldflags: + - -s -w # Don't set main.version. + +gomod: + proxy: true archives: - name_template: "{{.Binary}}_{{.Os}}_{{.Arch}}" diff --git a/CHANGELOG.md b/CHANGELOG.md index 335c24d7..c1e66788 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## Unreleased + +- Improve version reporting when building Task from source using Go Modules + ([#462](https://github.com/go-task/task/pull/462), [#473](https://github.com/go-task/task/pull/473)). + ## v3.4.1 - 2021-04-17 - Improve error reporting when parsing YAML: in some situations where you diff --git a/cmd/task/task.go b/cmd/task/task.go index 11d7ffab..e7071e1f 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -7,6 +7,7 @@ import ( "os" "os/signal" "path/filepath" + "runtime/debug" "strings" "syscall" @@ -19,7 +20,7 @@ import ( ) var ( - version = "master" + version = "" ) const usage = `Usage: task [-ilfwvsd] [--init] [--list] [--force] [--watch] [--verbose] [--silent] [--dir] [--taskfile] [--dry] [--summary] [task...] @@ -93,7 +94,7 @@ func main() { pflag.Parse() if versionFlag { - fmt.Printf("Task version: %s\n", version) + fmt.Printf("Task version: %s\n", getVersion()) return } @@ -217,3 +218,21 @@ func getSignalContext() context.Context { }() return ctx } + +func getVersion() string { + if version != "" { + return version + } + + info, ok := debug.ReadBuildInfo() + if !ok || info.Main.Version == "" { + return "unknown" + } + + version = info.Main.Version + if info.Main.Sum != "" { + version += fmt.Sprintf(" (%s)", info.Main.Sum) + } + + return version +}