From f59d2466e553a5c4e4afc006af96be2a85718e81 Mon Sep 17 00:00:00 2001 From: rkfg Date: Sun, 30 Oct 2022 13:43:25 +0300 Subject: [PATCH] Add version command --- main.go | 6 +++++- version.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 version.go diff --git a/main.go b/main.go index dc6fe1e..b09b146 100644 --- a/main.go +++ b/main.go @@ -52,6 +52,7 @@ type configParams struct { NodeCacheFilename string `long:"node-cache-filename" description:"save and load other nodes information to this file, improves cold start performance" json:"node_cache_filename" toml:"node_cache_filename"` NodeCacheLifetime int `long:"node-cache-lifetime" description:"nodes with last update older than this time (in minutes) will be removed from cache after loading it" json:"node_cache_lifetime" toml:"node_cache_lifetime"` NodeCacheInfo bool `long:"node-cache-info" description:"show red and cyan 'x' characters in routes to indicate node cache misses and hits respectively" json:"node_cache_info" toml:"node_cache_info"` + Version bool `short:"v" long:"version" description:"show program version and exit"` } var params, cfgParams configParams @@ -329,7 +330,10 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout } func preflightChecks(params *configParams) error { - + if params.Version { + printVersion() + os.Exit(1) + } if params.Connect == "" { params.Connect = "127.0.0.1:10009" } diff --git a/version.go b/version.go new file mode 100644 index 0000000..129f65c --- /dev/null +++ b/version.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "os" + "runtime/debug" +) + +func printVersion() { + info, ok := debug.ReadBuildInfo() + if !ok { + fmt.Printf("No build info available") + } else { + settings := map[string]string{} + for _, bs := range info.Settings { + settings[bs.Key] = bs.Value + } + version := info.Main.Version + if rev, ok := settings["vcs.revision"]; ok && version == "(devel)" { + version = "git" + rev[:8] + } + if settings["vcs.modified"] == "true" { + version += "-dirty" + } + fmt.Printf("Regolancer %s, built with %s\nSource: https://github.com/rkfg/regolancer\n", version, info.GoVersion) + } + os.Exit(1) +}