Skip to content

Commit

Permalink
include date and dirty flag in version
Browse files Browse the repository at this point in the history
  • Loading branch information
gammazero committed Aug 15, 2024
1 parent 768a7aa commit 8c4487c
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,47 @@ package main
import (
_ "embed"
"encoding/json"
"fmt"
"runtime/debug"
"time"
)

var version string

//go:embed version.json
var versionJSON []byte

func init() {
var version = buildVersion()

func buildVersion() string {
// Read version from embedded JSON file.
var verMap map[string]string
json.Unmarshal(versionJSON, &verMap)
version = verMap["version"]
release := verMap["version"]

// If running from a module, try to get the build info.
bi, ok := debug.ReadBuildInfo()
info, ok := debug.ReadBuildInfo()
if !ok {
return
return release + " dev-build"
}

var dirty bool
var day, revision string

// Append the revision to the version.
for i := range bi.Settings {
if bi.Settings[i].Key == "vcs.revision" {
version += "-" + bi.Settings[i].Value
break
for _, kv := range info.Settings {
switch kv.Key {
case "vcs.revision":
revision = kv.Value[:7]
case "vcs.time":
t, _ := time.Parse(time.RFC3339, kv.Value)
day = t.UTC().Format("2006-01-02")
case "vcs.modified":
dirty = kv.Value == "true"
}
}
if dirty {
revision += "-dirty"
}
if revision != "" {
return fmt.Sprintf("%s %s-%s", release, day, revision)
}
return release + " dev-build"
}

0 comments on commit 8c4487c

Please sign in to comment.