From 2dd3c6b526f15cb79fd2725c61dcdcb181b6fd6d Mon Sep 17 00:00:00 2001 From: Nate Finch Date: Fri, 25 Mar 2022 16:53:30 -0400 Subject: [PATCH 1/2] add version info automatically if you build with go 1.18+ --- mage/main.go | 11 ++---- mage/version.go | 86 +++++++++++++++++++++++++++++++++++++++++++++ mage/version_old.go | 14 ++++++++ 3 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 mage/version.go create mode 100644 mage/version_old.go diff --git a/mage/main.go b/mage/main.go index a41e7435..c78ed329 100644 --- a/mage/main.go +++ b/mage/main.go @@ -66,13 +66,6 @@ const ( var debug = log.New(ioutil.Discard, "DEBUG: ", log.Ltime|log.Lmicroseconds) -// set by ldflags when you "mage build" -var ( - commitHash = "" - timestamp = "" - gitTag = "" -) - //go:generate stringer -type=Command // Command tracks invocations of mage that run without targets or other flags. @@ -146,8 +139,8 @@ func ParseAndRun(stdout, stderr io.Writer, stdin io.Reader, args []string) int { switch cmd { case Version: - out.Println("Mage Build Tool", gitTag) - out.Println("Build Date:", timestamp) + out.Println("Mage Build Tool", getTag()) + out.Println("Commit Date:", timestamp) out.Println("Commit:", commitHash) out.Println("built with:", runtime.Version()) return 0 diff --git a/mage/version.go b/mage/version.go new file mode 100644 index 00000000..f4359281 --- /dev/null +++ b/mage/version.go @@ -0,0 +1,86 @@ +//go:build go1.18 + +package mage + +import ( + "encoding/json" + "net/http" + dbg "runtime/debug" + "sort" + "strings" + "time" +) + +// set by ldflags when you "mage build" +var ( + commitHash = "" + timestamp = "" + gitTag = "" +) + +func init() { + info, ok := dbg.ReadBuildInfo() + if !ok { + return + } + for _, kv := range info.Settings { + switch kv.Key { + case "vcs.revision": + commitHash = kv.Value + case "vcs.time": + timestamp = kv.Value + } + } +} + +// uses the commit hash to get the git tag (if one exists) from github +func getTag() string { + debug.Println("requesting tag info from github via https://api.github.com/repos/magefile/mage/git/refs/tags") + + http.DefaultClient.Timeout = 300 * time.Millisecond + resp, err := http.DefaultClient.Get("https://api.github.com/repos/magefile/mage/git/refs/tags") + if err != nil { + debug.Println("unable to request tag info from github:", err) + return "" + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + debug.Println("bad response requesting mage tag info from github:", resp.StatusCode) + return "" + } + + var tags []tag + err = json.NewDecoder(resp.Body).Decode(&tags) + if err != nil { + debug.Println("error unmarshalling mage tag info from github:", err) + return "" + } + + var found []string + for _, t := range tags { + if t.Object.Sha == commitHash && strings.HasPrefix(t.Ref, "refs/tags/") { + found = append(found, t.Ref[len("refs/tags/"):]) + } + } + if len(found) == 0 { + debug.Println("no git tag found for commit hash:", commitHash) + return "" + } + if len(found) == 1 { + return found[0] + } + // more than one tag for this commit, report the highest tag number. + sort.Strings(found) + return found[len(found)-1] +} + +type tag struct { + Ref string `json:"ref"` + NodeID string `json:"node_id"` + URL string `json:"url"` + Object struct { + Sha string `json:"sha"` + Type string `json:"type"` + URL string `json:"url"` + } `json:"object"` +} diff --git a/mage/version_old.go b/mage/version_old.go new file mode 100644 index 00000000..bcfcbc14 --- /dev/null +++ b/mage/version_old.go @@ -0,0 +1,14 @@ +//go:build !go1.18 + +package mage + +// set by ldflags when you "mage build" +var ( + commitHash = "" + timestamp = "" + gitTag = "" +) + +func getTag() string { + return gitTag +} From e748e07b8730e7fcea4f4345beec98722917c367 Mon Sep 17 00:00:00 2001 From: Nate Finch Date: Fri, 25 Mar 2022 17:08:00 -0400 Subject: [PATCH 2/2] fix build tags for old versions --- .github/workflows/ci.yml | 1 + mage/version.go | 1 + mage/version_old.go | 1 + 3 files changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ffbc5871..7463f9f7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,6 +9,7 @@ jobs: fail-fast: false matrix: go-version: + - 1.18.x - 1.17.x - 1.16.x - 1.15.x diff --git a/mage/version.go b/mage/version.go index f4359281..0a57ad7e 100644 --- a/mage/version.go +++ b/mage/version.go @@ -1,4 +1,5 @@ //go:build go1.18 +// +build go1.18 package mage diff --git a/mage/version_old.go b/mage/version_old.go index bcfcbc14..6979ac6e 100644 --- a/mage/version_old.go +++ b/mage/version_old.go @@ -1,4 +1,5 @@ //go:build !go1.18 +// +build !go1.18 package mage