From 2d7c3faa83f5675e62b89a7ccffd8c0318693231 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 27 May 2026 02:28:55 -0600 Subject: [PATCH] fix: scope version to sub-project, fix healthz to match --- cmd/check-ip/main.go | 28 +++++++++++++++++++++++++++- cmd/check-ip/server.go | 2 +- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/cmd/check-ip/main.go b/cmd/check-ip/main.go index 17ef89d..b7ff6f4 100644 --- a/cmd/check-ip/main.go +++ b/cmd/check-ip/main.go @@ -16,6 +16,7 @@ import ( "log" "net/http" "os" + "os/exec" "os/signal" "path/filepath" "strconv" @@ -44,6 +45,31 @@ var ( licenseType = "MPL-2.0" ) +// versionFromGit returns a tag scoped to this subdirectory, falling back to a short commit hash. +func versionFromGit() string { + if version != "0.0.0-dev" { + return version + } + out, err := runGit("describe", "--tags", "--match", "cmd/check-ip/*", "--always") + if err == nil && out != "" { + return out + } + out, err = runGit("rev-parse", "--short", "HEAD") + if err == nil && out != "" { + return "v0.0.0-" + out + } + return "0.0.0-dev" +} + +func runGit(args ...string) (string, error) { + cmd := exec.Command("git", args...) + out, err := cmd.Output() + if err != nil { + return "", err + } + return strings.TrimSpace(string(out)), nil +} + const ( defaultBlocklistRepo = "https://github.com/bitwire-it/ipblocklist.git" refreshInterval = 47 * time.Minute @@ -51,7 +77,7 @@ const ( // printVersion writes version, copyright, and license info to w. func printVersion(w io.Writer) { - _, _ = fmt.Fprintf(w, "%s v%s %s (%s)\n", name, version, commit[:7], date) + _, _ = fmt.Fprintf(w, "%s v%s %s (%s)\n", name, versionFromGit(), commit[:7], date) _, _ = fmt.Fprintf(w, "Copyright (C) %s %s\n", licenseYear, licenseOwner) _, _ = fmt.Fprintf(w, "Licensed under %s\n", licenseType) } diff --git a/cmd/check-ip/server.go b/cmd/check-ip/server.go index 330babe..2eeac02 100644 --- a/cmd/check-ip/server.go +++ b/cmd/check-ip/server.go @@ -200,7 +200,7 @@ func (c *IPCheck) healthz(w http.ResponseWriter, _ *http.Request) { Ready bool `json:"ready"` Version string `json:"version"` Datasets map[string]dsStatus `json:"datasets"` - }{ready, version, datasets} + }{ready, versionFromGit(), datasets} w.Header().Set("Content-Type", "application/json; charset=utf-8") if !ready {