diff --git a/Makefile b/Makefile index 3489a12..46f2f38 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/Taskfile.yml b/Taskfile.yml index f88cf9b..c96a173 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -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: diff --git a/cmd/q3rcon-proxy/main.go b/cmd/q3rcon-proxy/main.go index 19f0bca..0e08762 100644 --- a/cmd/q3rcon-proxy/main.go +++ b/cmd/q3rcon-proxy/main.go @@ -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", + 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.