chore: set version when built outside makefile

close #103
This commit is contained in:
Nicolas Carlier 2024-08-11 07:00:34 +00:00
parent 361770b898
commit 0c41f76f53

View File

@ -3,6 +3,8 @@ package version
import (
"flag"
"fmt"
"runtime/debug"
"time"
)
// Version of the app
@ -28,3 +30,26 @@ This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
`, Version, GitCommit, Built)
}
func init() {
if GitCommit != "snapshot" {
return
}
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
Version = info.Main.Version
for _, kv := range info.Settings {
if kv.Value == "" {
continue
}
switch kv.Key {
case "vcs.revision":
GitCommit = kv.Value[:7]
case "vcs.time":
lastCommit, _ := time.Parse(time.RFC3339, kv.Value)
Built = lastCommit.Format(time.RFC1123)
}
}
}