From 611cb92de7fcf2374d4553750904741535ec35ff Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Wed, 31 Jul 2024 14:55:55 -0500 Subject: [PATCH] feat: log version at startup (#178) Fixes #132 --- Makefile | 2 +- cmd/cdnsd/main.go | 7 ++++++- internal/version/version.go | 23 +++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 internal/version/version.go diff --git a/Makefile b/Makefile index e5a5749..8f16f19 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ BINARIES=$(shell cd $(ROOT_DIR)/cmd && ls -1 | grep -v ^common) GOMODULE=$(shell grep ^module $(ROOT_DIR)/go.mod | awk '{ print $$2 }') # Set version strings based on git tag and current ref -GO_LDFLAGS=-ldflags "-s -w" +GO_LDFLAGS=-ldflags "-s -w -X '$(GOMODULE)/internal/version.Version=$(shell git describe --tags --exact-match 2>/dev/null)' -X '$(GOMODULE)/internal/version.CommitHash=$(shell git rev-parse --short HEAD)'" .PHONY: build mod-tidy clean format golines test diff --git a/cmd/cdnsd/main.go b/cmd/cdnsd/main.go index 253ae4a..cc0906e 100644 --- a/cmd/cdnsd/main.go +++ b/cmd/cdnsd/main.go @@ -1,4 +1,4 @@ -// Copyright 2023 Blink Labs Software +// Copyright 2024 Blink Labs Software // // Use of this source code is governed by an MIT-style // license that can be found in the LICENSE file or at @@ -20,6 +20,7 @@ import ( "github.com/blinklabs-io/cdnsd/internal/indexer" "github.com/blinklabs-io/cdnsd/internal/logging" "github.com/blinklabs-io/cdnsd/internal/state" + "github.com/blinklabs-io/cdnsd/internal/version" ) var cmdlineFlags struct { @@ -54,6 +55,10 @@ func main() { } }() + logger.Info( + fmt.Sprintf("cdnsd %s started", version.GetVersionString()), + ) + // Load state if err := state.GetState().Load(); err != nil { logger.Fatalf("failed to load state: %s", err) diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..dd25da8 --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,23 @@ +// Copyright 2024 Blink Labs Software +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file or at +// https://opensource.org/licenses/MIT. + +package version + +import ( + "fmt" +) + +// These are populated at build time +var Version string +var CommitHash string + +func GetVersionString() string { + if Version != "" { + return fmt.Sprintf("%s (commit %s)", Version, CommitHash) + } else { + return fmt.Sprintf("devel (commit %s)", CommitHash) + } +}