From 2a3f049336f309d4f6d5a76e3d5d629b70cee8d8 Mon Sep 17 00:00:00 2001 From: Jamie Edge Date: Sun, 4 Apr 2021 12:57:58 +0100 Subject: [PATCH] Updated the version output to use Go module build information if available. Enabled GoReleaser module proxying for verifiable builds. --- .goreleaser.yml | 5 +++++ cmd/task/task.go | 25 +++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index e67d7b9f..07f3e719 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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/cmd/task/task.go b/cmd/task/task.go index 11d7ffab..eba118eb 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" @@ -18,9 +19,7 @@ import ( "github.com/go-task/task/v3/taskfile" ) -var ( - version = "master" -) +var version = "" const usage = `Usage: task [-ilfwvsd] [--init] [--list] [--force] [--watch] [--verbose] [--silent] [--dir] [--taskfile] [--dry] [--summary] [task...] @@ -93,7 +92,7 @@ func main() { pflag.Parse() if versionFlag { - fmt.Printf("Task version: %s\n", version) + fmt.Printf("Task version: %s\n", getVersion()) return } @@ -217,3 +216,21 @@ func getSignalContext() context.Context { }() return ctx } + +func getVersion() string { + if version != "" { + return version + } + + if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" { + version = info.Main.Version + + if info.Main.Sum != "" { + version += fmt.Sprintf(" (%s)", info.Main.Sum) + } + } else { + version = "unknown" + } + + return version +}