From 31172c0524d25336d8e07f116478da70af4d3ecb Mon Sep 17 00:00:00 2001 From: Sebastian Nickel Date: Tue, 17 Sep 2024 16:59:28 +0200 Subject: [PATCH] fix displaying of nctl version --- main.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index ac6031b..c022928 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "log" "os" "os/signal" + "runtime/debug" "strings" "syscall" @@ -50,10 +51,15 @@ type rootCommand struct { } const ( - version = "dev" defaultAPICluster = "nineapis.ch" ) +var ( + version string + commit string + date string +) + func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -171,7 +177,7 @@ func setupSignalHandler(ctx context.Context, cancel context.CancelFunc) { // checks for variables which would overwrite already existing ones. func kongVariables() (kong.Vars, error) { result := make(kong.Vars) - result["version"] = version + result["version"] = fmt.Sprintf("%s (commit: %s, date: %s)", version, commit, date) result["api_cluster"] = defaultAPICluster appCreateKongVars, err := create.ApplicationKongVars() if err != nil { @@ -197,3 +203,23 @@ func merge(existing kong.Vars, withs ...kong.Vars) error { return nil } + +func init() { + info, ok := debug.ReadBuildInfo() + if !ok { + return + } + + if version == "" { + version = info.Main.Version + } + + for _, kv := range info.Settings { + switch kv.Key { + case "vcs.revision": + commit = kv.Value + case "vcs.time": + date = kv.Value + } + } +}