add --version flag

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

View File

@@ -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,