mirror of
https://github.com/NotAShelf/watchdog.git
synced 2026-04-15 14:54:00 +00:00
cmd: infer version from version.json; add version flag
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I0f429ef3345444c0c4bedbc80ee3d5e06a6a6964
This commit is contained in:
parent
c1033bed39
commit
9efabc8f76
3 changed files with 53 additions and 4 deletions
|
|
@ -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)
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue