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
LINUX=$(BIN_DIR)/$(PROGRAM)_linux_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

View File

@ -11,8 +11,8 @@ vars:
WINDOWS: '{{.BIN_DIR}}/{{.PROGRAM}}_windows_amd64.exe'
LINUX: '{{.BIN_DIR}}/{{.PROGRAM}}_linux_amd64'
MACOS: '{{.BIN_DIR}}/{{.PROGRAM}}_darwin_amd64'
GIT_COMMIT:
sh: git log -n 1 --format=%h
VERSION:
sh: 'git describe --tags $(git rev-list --tags --max-count=1)'
tasks:
default:
@ -42,19 +42,19 @@ tasks:
build-windows:
desc: Build the q3rcon-proxy project for Windows
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
build-linux:
desc: Build the q3rcon-proxy project for Linux
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
build-macos:
desc: Build the q3rcon-proxy project for macOS
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
test:

View File

@ -5,6 +5,7 @@ import (
"context"
"fmt"
"os"
"runtime/debug"
"strconv"
"strings"
"time"
@ -15,6 +16,21 @@ import (
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.
type proxyConfig struct {
proxyHost string
@ -24,9 +40,14 @@ type proxyConfig struct {
}
func main() {
cli.VersionPrinter = func(cmd *cli.Command) {
fmt.Printf("q3rcon-proxy version: %s\n", cmd.Root().Version)
}
cmd := &cli.Command{
Name: "q3rcon-proxy",
Usage: "A Quake 3 RCON proxy server",
Version: versionFromBuild(),
Flags: []cli.Flag{
&cli.StringFlag{
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.