From 8eec34542a26b3c8da5edf882ddf4c9cce975363 Mon Sep 17 00:00:00 2001 From: Florian Weikert Date: Tue, 10 Dec 2024 14:57:56 +0100 Subject: [PATCH] Print Bazelisk version when startup flags are set. (#646) When invoking Bazelisk with the `version` command, the output should always start with the Bazelisk version followed by the output of `bazel version`. However, previously this was not the case when startup flags were set, e.g. when calling `bazelisk --nosystem_rc version`. --- core/core.go | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/core/core.go b/core/core.go index 1a15061..4205771 100644 --- a/core/core.go +++ b/core/core.go @@ -151,19 +151,9 @@ func RunBazeliskWithArgsFuncAndConfigAndOut(argsFunc ArgsFunc, repos *Repositori } } - // print bazelisk version information if "version" is the first argument + // print bazelisk version information if "version" is the first non-flag argument // bazel version is executed after this command - if len(args) > 0 && args[0] == "version" { - // Check if the --gnu_format flag is set, if that is the case, - // the version is printed differently - var gnuFormat bool - for _, arg := range args { - if arg == "--gnu_format" { - gnuFormat = true - break - } - } - + if ok, gnuFormat := isVersionCommand(args); ok { if gnuFormat { fmt.Printf("Bazelisk %s\n", BazeliskVersion) } else { @@ -178,6 +168,24 @@ func RunBazeliskWithArgsFuncAndConfigAndOut(argsFunc ArgsFunc, repos *Repositori return exitCode, nil } +func isVersionCommand(args []string) (result bool, gnuFormat bool) { + for _, arg := range args { + // Check if the --gnu_format flag is set, if that is the case, + // the version is printed differently + if arg == "--gnu_format" { + gnuFormat = true + } else if arg == "version" { + result = true + } else if !strings.HasPrefix(arg, "--") { + return // First non-flag arg is not "version" -> it must be a different command + } + if result && gnuFormat { + break + } + } + return +} + // BazelInstallation provides a summary of a single install of `bazel` type BazelInstallation struct { Version string