add --version flag

ensure version is injected at build time in Taskfile/makefile
This commit is contained in:
2026-02-16 01:49:28 +00:00
parent fc6ac8fa4e
commit a70fdecdbb
3 changed files with 32 additions and 9 deletions

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",
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.