diff --git a/cmd/watchdog/main.go b/cmd/watchdog/main.go index ed8c2da..de169ad 100644 --- a/cmd/watchdog/main.go +++ b/cmd/watchdog/main.go @@ -1,6 +1,7 @@ package watchdog import ( + "encoding/json" "fmt" "os" "strings" @@ -18,10 +19,53 @@ var ( buildDate string ) +type versionInfo struct { + Version string `json:"version"` + Commit string `json:"commit"` + BuildDate string `json:"buildDate"` +} + +func getVersion() string { + if version != "" { + return version + } + data, err := os.ReadFile("version.json") + if err != nil { + return "dev" + } + var v versionInfo + if err := json.Unmarshal(data, &v); err != nil { + return "dev" + } + if v.Version != "" { + return v.Version + } + return "dev" +} + +func getCommit() string { + if commit != "" { + return commit + } + data, err := os.ReadFile("version.json") + if err != nil { + return "none" + } + var v versionInfo + if err := json.Unmarshal(data, &v); err != nil { + return "none" + } + if v.Commit != "" { + return v.Commit + } + return "none" +} + var rootCmd = &cobra.Command{ - Use: "watchdog", - Short: "Privacy-first web analytics with Prometheus metrics", - Long: `Watchdog is a lightweight, privacy-first analytics system that aggregates web traffic data.`, + Use: "watchdog", + Short: "Privacy-first web analytics with Prometheus metrics", + Long: `Watchdog is a lightweight, privacy-first analytics system that aggregates web traffic data.`, + Version: getVersion() + "\ncommit: " + getCommit(), RunE: func(cmd *cobra.Command, args []string) error { return Run(cfg) }, diff --git a/cmd/watchdog/root.go b/cmd/watchdog/root.go index 8d2050f..fd0c98c 100644 --- a/cmd/watchdog/root.go +++ b/cmd/watchdog/root.go @@ -57,7 +57,7 @@ func Run(cfg *config.Config) error { promRegistry.MustRegister(blockedRequests) // Register health and runtime metrics - healthCollector := health.NewCollector(version, commit, buildDate) + healthCollector := health.NewCollector(getVersion(), getCommit(), buildDate) if err := healthCollector.Register(promRegistry); err != nil { return fmt.Errorf("failed to register health metrics: %w", err) } diff --git a/version.json b/version.json new file mode 100644 index 0000000..e42d58e --- /dev/null +++ b/version.json @@ -0,0 +1,5 @@ +{ + "version": "0.1.0", + "commit": "", + "buildDate": "" +}