Move to setting version info explicitly in linker flags
Some checks failed
/ golint (push) Failing after 12s

We don't have go built-in VCS information in a nix build because the git
repository isn't present. After struggling to build an overlay that
would provide it, I decided this path is easier of just injecting in the
data that we need.

Issue: #5
This commit is contained in:
Eli Ribble 2026-05-19 19:45:04 +00:00
parent 81826f853e
commit d4cbfb960e
No known key found for this signature in database
8 changed files with 119 additions and 79 deletions

70
main.go
View file

@ -9,6 +9,7 @@ import (
"os"
"os/signal"
"runtime/debug"
//"strconv"
"syscall"
"time"
@ -33,6 +34,13 @@ import (
"source.gleipnir.technology/Gleipnir/nidus-sync/version"
)
// These will be set by ldflags at build time by Nix
var (
BuildTime = "unknown"
Revision = "unknown"
Version = "dev"
)
func main() {
err := config.Parse()
if err != nil {
@ -46,8 +54,19 @@ func main() {
log.Warn().Msg("Forcing production mode for testing templates")
config.Environment = "PRODUCTION"
}
v := version.Get()
log.Info().Str("environment", config.Environment).Bool("is-prod", config.IsProductionEnvironment()).Str("revision", v.Revision).Str("build_time", v.BuildTime.String()).Bool("is modified", v.IsModified).Msg("Starting")
v := GetVersionInfo()
log.Info().
Str("environment", config.Environment).
Bool("is-prod", config.IsProductionEnvironment()).
Str("revision", v.Revision).
Str("build_time", v.BuildTime.String()).
Bool("is modified", v.IsModified).
Str("Version", Version).
Str("Revision", Revision).
Str("BuildDate", BuildTime).
Msg("Starting")
api.SetVersionInfo(v)
err = sentry.Init(sentry.ClientOptions{
Debug: false, //!config.IsProductionEnvironment(),
Dsn: config.SentryDSN,
@ -285,3 +304,50 @@ func LoggerMiddleware(logger *zerolog.Logger) func(next http.Handler) http.Handl
return http.HandlerFunc(fn)
}
}
func GetVersionInfo() (version.VersionInfo, error) {
// Try ldflags first (set by Nix build)
if Revision != "" && Revision != "unknown" {
build_time, err := parseNixTime(BuildTime)
if err != nil {
return version.VersionInfo{}, fmt.Errorf("parse nix time: %w", err)
}
return version.VersionInfo{
BuildTime: build_time,
IsModified: Revision == "dirty",
Revision: Revision,
}, nil
}
// Fallback to debug.ReadBuildInfo() for development
info, ok := debug.ReadBuildInfo()
if !ok {
return version.VersionInfo{}, fmt.Errorf("read build info not ok")
}
var v version.VersionInfo
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.modified":
v.IsModified = setting.Value == "true"
case "vcs.revision":
if len(setting.Value) > 7 {
v.Revision = setting.Value[:7]
} else {
v.Revision = setting.Value
}
case "vcs.time":
if t, err := time.Parse(time.RFC3339, setting.Value); err == nil {
v.BuildTime = t
}
}
}
return v, nil
}
func parseNixTime(timestamp string) (time.Time, error) {
layout := "20060102150405"
t, err := time.Parse(layout, timestamp)
if err != nil {
return time.Time{}, err
}
return t, nil
}