add --version flag

ensure version is injected at build time in makefile/taskfile
This commit is contained in:
onyx-and-iris 2026-02-16 00:55:17 +00:00
parent 3be7ddb36b
commit c4e2dacee9
3 changed files with 29 additions and 5 deletions

View File

@ -9,7 +9,7 @@ vars:
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: GIT_COMMIT:
sh: git log -n 1 --format=%h sh: 'git describe --tags $(git rev-list --tags --max-count=1)'
tasks: tasks:
default: default:
@ -39,19 +39,19 @@ tasks:
build-windows: build-windows:
desc: Build the q3rcon project for Windows desc: Build the q3rcon 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={{.GIT_COMMIT}}" ./cmd/{{.PROGRAM}}/
internal: true internal: true
build-linux: build-linux:
desc: Build the q3rcon project for Linux desc: Build the q3rcon 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={{.GIT_COMMIT}}" ./cmd/{{.PROGRAM}}/
internal: true internal: true
build-macos: build-macos:
desc: Build the q3rcon project for macOS desc: Build the q3rcon 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={{.GIT_COMMIT}}" ./cmd/{{.PROGRAM}}/
internal: true internal: true
test: test:

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"runtime/debug"
"strings" "strings"
"time" "time"
@ -14,6 +15,8 @@ import (
"github.com/onyx-and-iris/q3rcon" "github.com/onyx-and-iris/q3rcon"
) )
var version string // Version will be set at build time
func main() { func main() {
var exitCode int var exitCode int
@ -42,6 +45,7 @@ func run() (func(), error) {
rconpass string rconpass string
interactive bool interactive bool
loglevel string loglevel string
versionFlag bool
) )
flag.StringVar(&host, "host", "localhost", "hostname of the gameserver") flag.StringVar(&host, "host", "localhost", "hostname of the gameserver")
@ -67,8 +71,16 @@ func run() (func(), error) {
flag.StringVar(&loglevel, "loglevel", "warn", "log level") flag.StringVar(&loglevel, "loglevel", "warn", "log level")
flag.StringVar(&loglevel, "l", "warn", "log level (shorthand)") flag.StringVar(&loglevel, "l", "warn", "log level (shorthand)")
flag.BoolVar(&versionFlag, "version", false, "print version information and exit")
flag.BoolVar(&versionFlag, "v", false, "print version information and exit (shorthand)")
flag.Parse() flag.Parse()
if versionFlag {
fmt.Printf("q3rcon version: %s\n", versionFromBuild())
return nil, nil
}
level, err := log.ParseLevel(loglevel) level, err := log.ParseLevel(loglevel)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid log level: %s", loglevel) return nil, fmt.Errorf("invalid log level: %s", loglevel)
@ -110,6 +122,18 @@ func run() (func(), error) {
return closer, nil return closer, nil
} }
// versionFromBuild retrieves the version information from the build metadata.
func versionFromBuild() string {
if version == "" {
info, ok := debug.ReadBuildInfo()
if !ok {
return "(unable to read build info)"
}
version = strings.Split(info.Main.Version, "-")[0]
}
return version
}
func connectRcon(host string, port int, password string) (*q3rcon.Rcon, func(), error) { func connectRcon(host string, port int, password string) (*q3rcon.Rcon, func(), error) {
client, err := q3rcon.New(host, port, password, q3rcon.WithTimeouts(map[string]time.Duration{ client, err := q3rcon.New(host, port, password, q3rcon.WithTimeouts(map[string]time.Duration{
"map": time.Second, "map": time.Second,

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