Use goreleaser version if available, otherwise buildinfo

This commit is contained in:
Samuel Lorch 2024-04-05 21:45:57 +02:00
parent b269e8538d
commit b48f3274e6
3 changed files with 48 additions and 6 deletions

View file

@ -121,6 +121,10 @@ func initConfig() {
}
}
func SetVersionInfo(version, commit, date string) {
rootCmd.Version = fmt.Sprintf("%s (Built on %s from Git SHA %s)", version, date, commit)
func SetVersionInfo(version, commit, date string, dirty bool) {
v := fmt.Sprintf("%s (Built on %s from Git SHA %s)", version, date, commit)
if dirty {
v = v + " dirty"
}
rootCmd.Version = v
}

View file

@ -1,13 +1,10 @@
package main
import (
"time"
"github.com/carlmjohnson/versioninfo"
"github.com/passbolt/go-passbolt-cli/cmd"
)
func main() {
cmd.SetVersionInfo(versioninfo.Version, versioninfo.Revision, versioninfo.LastCommit.Format(time.RFC3339))
cmd.SetVersionInfo(version, commit, date, dirty)
cmd.Execute()
}

41
version.go Normal file
View file

@ -0,0 +1,41 @@
package main
import (
"runtime/debug"
"time"
)
var (
version = "unknown"
commit = "unknown"
date = "unknown"
dirty = false
)
func init() {
// if not set by goreleaser, use buildinfo instead
if version == "unknown" {
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
if info.Main.Version != "" {
version = info.Main.Version
}
for _, kv := range info.Settings {
if kv.Value == "" {
continue
}
switch kv.Key {
case "vcs.revision":
commit = kv.Value
case "vcs.time":
d, _ := time.Parse(time.RFC3339, kv.Value)
date = d.String()
case "vcs.modified":
dirty = kv.Value == "true"
}
}
}
}