mirror of
https://github.com/NotAShelf/watchdog.git
synced 2026-04-23 10:05:11 +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
|
package watchdog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -18,10 +19,53 @@ var (
|
||||||
buildDate string
|
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{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "watchdog",
|
Use: "watchdog",
|
||||||
Short: "Privacy-first web analytics with Prometheus metrics",
|
Short: "Privacy-first web analytics with Prometheus metrics",
|
||||||
Long: `Watchdog is a lightweight, privacy-first analytics system that aggregates web traffic data.`,
|
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 {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return Run(cfg)
|
return Run(cfg)
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ func Run(cfg *config.Config) error {
|
||||||
promRegistry.MustRegister(blockedRequests)
|
promRegistry.MustRegister(blockedRequests)
|
||||||
|
|
||||||
// Register health and runtime metrics
|
// Register health and runtime metrics
|
||||||
healthCollector := health.NewCollector(version, commit, buildDate)
|
healthCollector := health.NewCollector(getVersion(), getCommit(), buildDate)
|
||||||
if err := healthCollector.Register(promRegistry); err != nil {
|
if err := healthCollector.Register(promRegistry); err != nil {
|
||||||
return fmt.Errorf("failed to register health metrics: %w", err)
|
return fmt.Errorf("failed to register health metrics: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
5
version.json
Normal file
5
version.json
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"version": "0.1.0",
|
||||||
|
"commit": "",
|
||||||
|
"buildDate": ""
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue