add --version flag

ensure version is injected at build time in Taskfile/makefile
This commit is contained in:
onyx-and-iris 2026-02-16 01:49:28 +00:00
parent fc6ac8fa4e
commit a70fdecdbb
3 changed files with 32 additions and 9 deletions

View File

@ -6,7 +6,7 @@ BIN_DIR := bin
WINDOWS=$(BIN_DIR)/$(PROGRAM)_windows_amd64.exe WINDOWS=$(BIN_DIR)/$(PROGRAM)_windows_amd64.exe
LINUX=$(BIN_DIR)/$(PROGRAM)_linux_amd64 LINUX=$(BIN_DIR)/$(PROGRAM)_linux_amd64
MACOS=$(BIN_DIR)/$(PROGRAM)_darwin_amd64 MACOS=$(BIN_DIR)/$(PROGRAM)_darwin_amd64
VERSION=$(shell git log -n 1 --format=%h) VERSION=$(shell git describe --tags $(shell git rev-list --tags --max-count=1))
.DEFAULT_GOAL := build .DEFAULT_GOAL := build

View File

@ -11,8 +11,8 @@ vars:
WINDOWS: '{{.BIN_DIR}}/{{.PROGRAM}}_windows_amd64.exe' WINDOWS: '{{.BIN_DIR}}/{{.PROGRAM}}_windows_amd64.exe'
LINUX: '{{.BIN_DIR}}/{{.PROGRAM}}_linux_amd64' LINUX: '{{.BIN_DIR}}/{{.PROGRAM}}_linux_amd64'
MACOS: '{{.BIN_DIR}}/{{.PROGRAM}}_darwin_amd64' MACOS: '{{.BIN_DIR}}/{{.PROGRAM}}_darwin_amd64'
GIT_COMMIT: VERSION:
sh: git log -n 1 --format=%h sh: 'git describe --tags $(git rev-list --tags --max-count=1)'
tasks: tasks:
default: default:
@ -42,19 +42,19 @@ tasks:
build-windows: build-windows:
desc: Build the q3rcon-proxy project for Windows desc: Build the q3rcon-proxy project for Windows
cmds: cmds:
- GOOS=windows GOARCH=amd64 go build -o {{.WINDOWS}} -ldflags="-X main.Version={{.GIT_COMMIT}}" ./cmd/{{.PROGRAM}}/ - GOOS=windows GOARCH=amd64 go build -o {{.WINDOWS}} -ldflags="-X main.version={{.VERSION}}" ./cmd/{{.PROGRAM}}/
internal: true internal: true
build-linux: build-linux:
desc: Build the q3rcon-proxy project for Linux desc: Build the q3rcon-proxy project for Linux
cmds: cmds:
- GOOS=linux GOARCH=amd64 go build -o {{.LINUX}} -ldflags="-X main.Version={{.GIT_COMMIT}}" ./cmd/{{.PROGRAM}}/ - GOOS=linux GOARCH=amd64 go build -o {{.LINUX}} -ldflags="-X main.version={{.VERSION}}" ./cmd/{{.PROGRAM}}/
internal: true internal: true
build-macos: build-macos:
desc: Build the q3rcon-proxy project for macOS desc: Build the q3rcon-proxy project for macOS
cmds: cmds:
- GOOS=darwin GOARCH=amd64 go build -o {{.MACOS}} -ldflags="-X main.Version={{.GIT_COMMIT}}" ./cmd/{{.PROGRAM}}/ - GOOS=darwin GOARCH=amd64 go build -o {{.MACOS}} -ldflags="-X main.version={{.VERSION}}" ./cmd/{{.PROGRAM}}/
internal: true internal: true
test: test:

View File

@ -5,6 +5,7 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"runtime/debug"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -15,6 +16,21 @@ import (
udpproxy "github.com/onyx-and-iris/q3rcon-proxy" udpproxy "github.com/onyx-and-iris/q3rcon-proxy"
) )
var version string // Version holds the application version, set at build time using ldflags.
// versionFromBuild retrieves the version information from the build metadata.
func versionFromBuild() string {
if version != "" {
return version
}
info, ok := debug.ReadBuildInfo()
if !ok {
return "(unable to read version)"
}
return strings.Split(info.Main.Version, "-")[0]
}
// proxyConfig holds the configuration for a single UDP proxy server. // proxyConfig holds the configuration for a single UDP proxy server.
type proxyConfig struct { type proxyConfig struct {
proxyHost string proxyHost string
@ -24,9 +40,14 @@ type proxyConfig struct {
} }
func main() { func main() {
cli.VersionPrinter = func(cmd *cli.Command) {
fmt.Printf("q3rcon-proxy version: %s\n", cmd.Root().Version)
}
cmd := &cli.Command{ cmd := &cli.Command{
Name: "q3rcon-proxy", Name: "q3rcon-proxy",
Usage: "A Quake 3 RCON proxy server", Usage: "A Quake 3 RCON proxy server",
Version: versionFromBuild(),
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "proxy-host", Name: "proxy-host",
@ -111,7 +132,9 @@ func main() {
}, },
} }
log.Fatal(cmd.Run(context.Background(), os.Args)) if err := cmd.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
} }
// launchProxy initialises the UDP proxy server with the given configuration. // launchProxy initialises the UDP proxy server with the given configuration.