Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add version info automatically if you build with go 1.18+ #417

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jobs:
fail-fast: false
matrix:
go-version:
- 1.18.x
- 1.17.x
- 1.16.x
- 1.15.x
Expand Down
11 changes: 2 additions & 9 deletions mage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<not set>"
timestamp = "<not set>"
gitTag = "<not set>"
)

//go:generate stringer -type=Command

// Command tracks invocations of mage that run without targets or other flags.
Expand Down Expand Up @@ -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
Expand Down
87 changes: 87 additions & 0 deletions mage/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//go:build go1.18
// +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 = "<not set>"
timestamp = "<not set>"
gitTag = "<not set>"
)

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 "<no tag for this commit>"
}
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"`
}
15 changes: 15 additions & 0 deletions mage/version_old.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build !go1.18
// +build !go1.18

package mage

// set by ldflags when you "mage build"
var (
commitHash = "<not set>"
timestamp = "<not set>"
gitTag = ""
)

func getTag() string {
return gitTag
}