From 49a6c03c09f1eb98fc46a42e2b8cbc95843a480e Mon Sep 17 00:00:00 2001 From: Christian Simon Date: Tue, 2 Aug 2022 13:06:55 +0100 Subject: [PATCH] Treat version flag at any position (#6827) The behaviour was changed as part of #6802. This should restore the previous behaviour and allow the version to be displayed without config file. --- cmd/loki/main.go | 2 +- pkg/loki/config_wrapper.go | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/cmd/loki/main.go b/cmd/loki/main.go index ba07c9cb30..9a089b3958 100644 --- a/cmd/loki/main.go +++ b/cmd/loki/main.go @@ -24,7 +24,7 @@ import ( func main() { var config loki.ConfigWrapper - if len(os.Args) >= 1 && os.Args[1] == "-version" { + if loki.PrintVersion(os.Args[1:]) { fmt.Println(version.Print("loki")) os.Exit(0) } diff --git a/pkg/loki/config_wrapper.go b/pkg/loki/config_wrapper.go index 47d75a61ff..d34431da3a 100644 --- a/pkg/loki/config_wrapper.go +++ b/pkg/loki/config_wrapper.go @@ -20,13 +20,15 @@ import ( loki_net "github.com/grafana/loki/pkg/util/net" ) +const versionFlag = "version" + // ConfigWrapper is a struct containing the Loki config along with other values that can be set on the command line // for interacting with the config file or the application directly. // ConfigWrapper implements cfg.DynamicCloneable, allowing configuration to be dynamically set based // on the logic in ApplyDynamicConfig, which receives values set in config file type ConfigWrapper struct { Config `yaml:",inline"` - PrintVersion bool + printVersion bool VerifyConfig bool PrintConfig bool ListTargets bool @@ -35,8 +37,17 @@ type ConfigWrapper struct { ConfigExpandEnv bool } +func PrintVersion(args []string) bool { + for _, a := range args { + if a == "-"+versionFlag { + return true + } + } + return false +} + func (c *ConfigWrapper) RegisterFlags(f *flag.FlagSet) { - f.BoolVar(&c.PrintVersion, "version", false, "Print this builds version information") + f.BoolVar(&c.printVersion, versionFlag, false, "Print this builds version information") f.BoolVar(&c.VerifyConfig, "verify-config", false, "Verify config file and exits") f.BoolVar(&c.PrintConfig, "print-config-stderr", false, "Dump the entire Loki config object to stderr") f.BoolVar(&c.ListTargets, "list-targets", false, "List available targets")