From 10a70df93a73c133555b7184a0c3ef8f1f45f36b Mon Sep 17 00:00:00 2001 From: Andrew Gillis <11790789+gammazero@users.noreply.github.com> Date: Thu, 15 Aug 2024 14:45:42 -0700 Subject: [PATCH] App version is taken from version.json compiled in (#149) * App version is taken from version.json compiled in This ensures that the version is always set to the correct value. making version.json the source of truth. Fixes #146 Fixes #113 * remove version number from README * include date and dirty flag in version --- README.md | 2 +- main.go | 2 +- version.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 version.go diff --git a/README.md b/README.md index 5f433b4..abfbd57 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ USAGE: ipget [global options] command [command options] [arguments...] VERSION: - 0.9.2 + see version.json COMMANDS: help, h Shows a list of commands or help for one command diff --git a/main.go b/main.go index 4427c66..9a8b4cf 100644 --- a/main.go +++ b/main.go @@ -32,7 +32,7 @@ func main() { app := cli.NewApp() app.Name = "ipget" app.Usage = "Retrieve and save IPFS objects." - app.Version = "0.9.2" + app.Version = version app.Flags = []cli.Flag{ &cli.StringFlag{ Name: "output", diff --git a/version.go b/version.go new file mode 100644 index 0000000..357c633 --- /dev/null +++ b/version.go @@ -0,0 +1,51 @@ +package main + +import ( + _ "embed" + "encoding/json" + "fmt" + "runtime/debug" + "time" +) + +//go:embed version.json +var versionJSON []byte + +var version = buildVersion() + +func buildVersion() string { + // Read version from embedded JSON file. + var v struct { + Version string `json:"version"` + } + json.Unmarshal(versionJSON, &v) + release := v.Version + + info, ok := debug.ReadBuildInfo() + if !ok { + return release + " dev-build" + } + + var dirty bool + var day, revision string + + // Append the revision to the version. + 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" +}