Skip to content

Commit

Permalink
App version is taken from version.json compiled in (#149)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gammazero authored Aug 15, 2024
1 parent 2c06a6c commit 10a70df
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
51 changes: 51 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 10a70df

Please sign in to comment.