From 1dc1300492b8db8222f749cfa4e0e436d2e14c65 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 26 Mar 2026 09:41:38 +0000 Subject: [PATCH] Implement build-time version injection This commit implements build-time version injection for the netscan service by moving the application version variable to a dedicated `internal/version` package. This ensures the version is accessible to both the main application and the health check API response. Key changes: - Created `internal/version` package for central version management. - Removed `cmd/netscan/version.go`. - Updated `cmd/netscan/main.go` to include a `-version` flag and log the version on startup. - Updated `cmd/netscan/health.go` to use the dynamic version in the API response. - Updated `scripts/build.sh`, `Dockerfile`, and `deploy/deploy.sh` to inject the version string via `-ldflags`. - Updated `MANUAL.md` to reflect that the version is now injected at build time. - Set default fallback version to '1.0.0' in all build scripts and the code. Co-authored-by: kljama <176691597+kljama@users.noreply.github.com> --- Dockerfile | 4 ++-- MANUAL.md | 2 +- cmd/netscan/health.go | 3 ++- cmd/netscan/main.go | 10 +++++++++- deploy/deploy.sh | 4 ++-- {cmd/netscan => internal/version}/version.go | 2 +- scripts/build.sh | 4 ++-- 7 files changed, 19 insertions(+), 10 deletions(-) rename {cmd/netscan => internal/version}/version.go (86%) diff --git a/Dockerfile b/Dockerfile index 559f268..43fc058 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,11 +16,11 @@ RUN go mod download COPY . . # Version build argument -ARG VERSION=dev +ARG VERSION=1.0.0 # Build the binary with optimizations for linux/amd64 RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ - -ldflags="-w -s -X main.Version=${VERSION}" \ + -ldflags="-w -s -X github.com/kljama/netscan/internal/version.Version=${VERSION}" \ -o netscan \ ./cmd/netscan diff --git a/MANUAL.md b/MANUAL.md index ef42fb2..b26b6ea 100644 --- a/MANUAL.md +++ b/MANUAL.md @@ -1388,7 +1388,7 @@ netscan exposes HTTP health check endpoints for monitoring, container orchestrat | Field | Type | Description | |-------|------|-------------| | `status` | string | Overall service health: `"healthy"` (all systems operational), `"degraded"` (InfluxDB unreachable but monitoring continues), or `"unhealthy"` (critical failure) | -| `version` | string | Application version string (currently hardcoded `"1.0.0"`, TODO: inject at build time) | +| `version` | string | Application version string (injected at build time via ldflags) | | `uptime` | string | Human-readable time since service started (e.g., `"2h15m30s"`) | | `device_count` | int | Total number of devices currently managed by StateManager | | `snmp_suspended_devices` | int | Number of devices currently suspended by SNMP circuit breaker | diff --git a/cmd/netscan/health.go b/cmd/netscan/health.go index 0136d7c..4806417 100644 --- a/cmd/netscan/health.go +++ b/cmd/netscan/health.go @@ -13,6 +13,7 @@ import ( "github.com/kljama/netscan/internal/influx" "github.com/kljama/netscan/internal/state" + "github.com/kljama/netscan/internal/version" "github.com/rs/zerolog/log" ) @@ -113,7 +114,7 @@ func (hs *HealthServer) GetHealthMetrics() HealthResponse { return HealthResponse{ Status: status, - Version: Version, + Version: version.Version, Uptime: time.Since(hs.startTime).String(), DeviceCount: hs.stateMgr.Count(), SNMPSuspendedDevices: hs.stateMgr.GetSNMPSuspendedCount(), diff --git a/cmd/netscan/main.go b/cmd/netscan/main.go index 1dca380..22ebdfe 100644 --- a/cmd/netscan/main.go +++ b/cmd/netscan/main.go @@ -3,6 +3,7 @@ package main import ( "context" "flag" + "fmt" "os" "os/signal" "runtime" @@ -17,18 +18,25 @@ import ( "github.com/kljama/netscan/internal/logger" "github.com/kljama/netscan/internal/monitoring" "github.com/kljama/netscan/internal/state" + "github.com/kljama/netscan/internal/version" "github.com/rs/zerolog/log" "golang.org/x/time/rate" ) func main() { configPath := flag.String("config", "config.yml", "Path to configuration file") + showVersion := flag.Bool("version", false, "Print version and exit") flag.Parse() + if *showVersion { + fmt.Printf("netscan version %s\n", version.Version) + return + } + // Initialize structured logging logger.Setup(false) // Set to true for debug mode - log.Info().Msg("netscan starting up...") + log.Info().Str("version", version.Version).Msg("netscan starting up...") cfg, err := config.LoadConfig(*configPath) if err != nil { log.Fatal().Err(err).Msg("failed to load config") diff --git a/deploy/deploy.sh b/deploy/deploy.sh index bcaede7..79f4440 100755 --- a/deploy/deploy.sh +++ b/deploy/deploy.sh @@ -64,10 +64,10 @@ build_binary() { # Get version info local version - version=$(git describe --tags --always --dirty 2>/dev/null || echo "dev") + version=$(git describe --tags --always --dirty 2>/dev/null || echo "1.0.0") log_info "Building version: $version" - if ! go build -ldflags "-X main.Version=$version" -o "$BINARY" ./cmd/netscan; then + if ! go build -ldflags "-X github.com/kljama/netscan/internal/version.Version=$version" -o "$BINARY" ./cmd/netscan; then error_exit "Failed to build netscan binary" fi diff --git a/cmd/netscan/version.go b/internal/version/version.go similarity index 86% rename from cmd/netscan/version.go rename to internal/version/version.go index 29640d2..3c6c408 100644 --- a/cmd/netscan/version.go +++ b/internal/version/version.go @@ -1,4 +1,4 @@ -package main +package version // Version is the application version. // It is set at build time using -ldflags. diff --git a/scripts/build.sh b/scripts/build.sh index 7da11b5..42c1883 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -12,7 +12,7 @@ BINARY=netscan cd "$PROJECT_ROOT" # Determine version -VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev") +VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "1.0.0") echo "Building version: $VERSION" # Build the binary @@ -22,6 +22,6 @@ if [ -f "$BINARY" ]; then fi echo "Building netscan..." -go build -ldflags "-X main.Version=$VERSION" -o $BINARY ./cmd/netscan +go build -ldflags "-X github.com/kljama/netscan/internal/version.Version=$VERSION" -o $BINARY ./cmd/netscan echo "Build complete: $BINARY"