Add setting and getting version information

- Add `Version` string var to config package for storing version information.
- Update Makefile to set the `Version` variable at build time using the latest Git tag.
- Add `-version` flag to main package to print version information and exit.
- Remove setting the GO111MODULE environment variable from Makefile when building. It's not needed.
This commit is contained in:
Ryan Smith
2025-03-22 08:56:10 -07:00
parent f6cd4c783e
commit 7dc7b1ee83
3 changed files with 36 additions and 24 deletions

View File

@@ -1,24 +1,24 @@
# Makefile for Deceptifeed # Makefile for Deceptifeed
SOURCE = ./cmd/deceptifeed/ SOURCE := ./cmd/deceptifeed/
BIN_DIRECTORY = ./bin/ BIN_DIRECTORY := ./bin/
BIN_DEFAULT = deceptifeed BIN_DEFAULT := deceptifeed
BIN_LINUX = $(BIN_DEFAULT)_linux_amd64 BIN_LINUX := $(BIN_DEFAULT)_linux_amd64
BIN_FREEBSD = $(BIN_DEFAULT)_freebsd_amd64 BIN_FREEBSD := $(BIN_DEFAULT)_freebsd_amd64
BIN_WINDOWS = $(BIN_DEFAULT)_windows_amd64.exe BIN_WINDOWS := $(BIN_DEFAULT)_windows_amd64.exe
INSTALL_SCRIPT = ./scripts/install.sh INSTALL_SCRIPT := ./scripts/install.sh
UNINSTALL_SCRIPT = ./scripts/install.sh --uninstall UNINSTALL_SCRIPT := ./scripts/install.sh --uninstall
BUILD_OPTIONS = -trimpath -ldflags="-s -w" VERSION := $(shell git describe --tags)
GO = go BUILD_OPTIONS := -trimpath -ldflags="-s -w -X 'github.com/r-smith/deceptifeed/internal/config.Version=$(VERSION)'"
CGO_ENABLED = 0 GO := go
GO111MODULE = on CGO_ENABLED := 0
.PHONY: build .PHONY: build
build: build:
@echo "Building for current operating system to: $(BIN_DIRECTORY)$(BIN_DEFAULT)" @echo "Building for current operating system..."
@mkdir -p $(BIN_DIRECTORY) @mkdir -p $(BIN_DIRECTORY)
GO111MODULE=$(GO111MODULE) CGO_ENABLED=$(CGO_ENABLED) $(GO) build $(BUILD_OPTIONS) -o $(BIN_DIRECTORY)$(BIN_DEFAULT) $(SOURCE) CGO_ENABLED=$(CGO_ENABLED) $(GO) build $(BUILD_OPTIONS) -o $(BIN_DIRECTORY)$(BIN_DEFAULT) $(SOURCE)
@echo "Build complete." @echo "Build complete: $(BIN_DIRECTORY)$(BIN_DEFAULT)"
@echo @echo
.PHONY: all .PHONY: all
@@ -26,26 +26,26 @@ all: build build-linux build-freebsd build-windows
.PHONY: build-linux .PHONY: build-linux
build-linux: build-linux:
@echo "Building for Linux to: $(BIN_DIRECTORY)$(BIN_LINUX)" @echo "Building for Linux..."
@mkdir -p $(BIN_DIRECTORY) @mkdir -p $(BIN_DIRECTORY)
GOOS=linux GOARCH=amd64 GO111MODULE=$(GO111MODULE) CGO_ENABLED=$(CGO_ENABLED) $(GO) build $(BUILD_OPTIONS) -o $(BIN_DIRECTORY)$(BIN_LINUX) $(SOURCE) GOOS=linux GOARCH=amd64 CGO_ENABLED=$(CGO_ENABLED) $(GO) build $(BUILD_OPTIONS) -o $(BIN_DIRECTORY)$(BIN_LINUX) $(SOURCE)
@echo "Build complete." @echo "Build complete: $(BIN_DIRECTORY)$(BIN_LINUX)"
@echo @echo
.PHONY: build-freebsd .PHONY: build-freebsd
build-freebsd: build-freebsd:
@echo "Building for FreeBSD to: $(BIN_DIRECTORY)$(BIN_FREEBSD)" @echo "Building for FreeBSD..."
@mkdir -p $(BIN_DIRECTORY) @mkdir -p $(BIN_DIRECTORY)
GOOS=freebsd GOARCH=amd64 GO111MODULE=$(GO111MODULE) CGO_ENABLED=$(CGO_ENABLED) $(GO) build $(BUILD_OPTIONS) -o $(BIN_DIRECTORY)$(BIN_FREEBSD) $(SOURCE) GOOS=freebsd GOARCH=amd64 CGO_ENABLED=$(CGO_ENABLED) $(GO) build $(BUILD_OPTIONS) -o $(BIN_DIRECTORY)$(BIN_FREEBSD) $(SOURCE)
@echo "Build complete." @echo "Build complete: $(BIN_DIRECTORY)$(BIN_FREEBSD)"
@echo @echo
.PHONY: build-windows .PHONY: build-windows
build-windows: build-windows:
@echo "Building for Windows to: $(BIN_DIRECTORY)$(BIN_WINDOWS)" @echo "Building for Windows..."
@mkdir -p $(BIN_DIRECTORY) @mkdir -p $(BIN_DIRECTORY)
GOOS=windows GOARCH=amd64 GO111MODULE=$(GO111MODULE) CGO_ENABLED=$(CGO_ENABLED) $(GO) build $(BUILD_OPTIONS) -o $(BIN_DIRECTORY)$(BIN_WINDOWS) $(SOURCE) GOOS=windows GOARCH=amd64 CGO_ENABLED=$(CGO_ENABLED) $(GO) build $(BUILD_OPTIONS) -o $(BIN_DIRECTORY)$(BIN_WINDOWS) $(SOURCE)
@echo "Build complete." @echo "Build complete: $(BIN_DIRECTORY)$(BIN_WINDOWS)"
@echo @echo
.PHONY: install .PHONY: install

View File

@@ -43,8 +43,15 @@ func main() {
flag.StringVar(&https.CertPath, "https-cert", config.DefaultCertPathHTTPS, "Path to optional TLS public certificate") flag.StringVar(&https.CertPath, "https-cert", config.DefaultCertPathHTTPS, "Path to optional TLS public certificate")
flag.StringVar(&https.KeyPath, "https-key", config.DefaultKeyPathHTTPS, "Path to optional TLS private key") flag.StringVar(&https.KeyPath, "https-key", config.DefaultKeyPathHTTPS, "Path to optional TLS private key")
flag.StringVar(&ssh.KeyPath, "ssh-key", config.DefaultKeyPathSSH, "Path to optional SSH private key") flag.StringVar(&ssh.KeyPath, "ssh-key", config.DefaultKeyPathSSH, "Path to optional SSH private key")
ver := flag.Bool("version", false, "Output the version number and exit")
flag.Parse() flag.Parse()
// If the `-version` flag is provided, output the version number and exit.
if *ver {
fmt.Println(config.Version)
return
}
// If the `-config` flag is not provided, use "config.xml" from the current // If the `-config` flag is not provided, use "config.xml" from the current
// directory if the file exists. // directory if the file exists.
if len(*configPath) == 0 { if len(*configPath) == 0 {

View File

@@ -12,6 +12,11 @@ import (
"github.com/r-smith/deceptifeed/internal/logrotate" "github.com/r-smith/deceptifeed/internal/logrotate"
) )
// Version stores Deceptifeed's version number. This variable is set at build
// time using the `-X` option with `-ldflags` and is assigned the latest Git
// tag. Refer to the Makefile in the project root for details on how it's set.
var Version = "undefined"
// This block of constants defines the default application settings when no // This block of constants defines the default application settings when no
// configuration file is provided. // configuration file is provided.
const ( const (