diff --git a/Taskfile.yml b/Taskfile.yml index 03bb5dc..ed45bb0 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -9,7 +9,7 @@ vars: LINUX: '{{.BIN_DIR}}/{{.PROGRAM}}_linux_amd64' MACOS: '{{.BIN_DIR}}/{{.PROGRAM}}_darwin_amd64' GIT_COMMIT: - sh: git log -n 1 --format=%h + sh: 'git describe --tags $(git rev-list --tags --max-count=1)' tasks: default: @@ -39,19 +39,19 @@ tasks: build-windows: desc: Build the q3rcon 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={{.GIT_COMMIT}}" ./cmd/{{.PROGRAM}}/ internal: true build-linux: desc: Build the q3rcon 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={{.GIT_COMMIT}}" ./cmd/{{.PROGRAM}}/ internal: true build-macos: desc: Build the q3rcon 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={{.GIT_COMMIT}}" ./cmd/{{.PROGRAM}}/ internal: true test: diff --git a/cmd/q3rcon/main.go b/cmd/q3rcon/main.go index f8514df..ac9d3f9 100644 --- a/cmd/q3rcon/main.go +++ b/cmd/q3rcon/main.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "os" + "runtime/debug" "strings" "time" @@ -14,6 +15,8 @@ import ( "github.com/onyx-and-iris/q3rcon" ) +var version string // Version will be set at build time + func main() { var exitCode int @@ -42,6 +45,7 @@ func run() (func(), error) { rconpass string interactive bool loglevel string + versionFlag bool ) 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, "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() + if versionFlag { + fmt.Printf("q3rcon version: %s\n", versionFromBuild()) + return nil, nil + } + level, err := log.ParseLevel(loglevel) if err != nil { return nil, fmt.Errorf("invalid log level: %s", loglevel) @@ -110,6 +122,18 @@ func run() (func(), error) { 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) { client, err := q3rcon.New(host, port, password, q3rcon.WithTimeouts(map[string]time.Duration{ "map": time.Second, diff --git a/makefile b/makefile index 3eec03f..0743249 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