Skip to content

Commit 8eec345

Browse files
authored
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`.
1 parent 86ef884 commit 8eec345

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

core/core.go

+20-12
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,9 @@ func RunBazeliskWithArgsFuncAndConfigAndOut(argsFunc ArgsFunc, repos *Repositori
151151
}
152152
}
153153

154-
// print bazelisk version information if "version" is the first argument
154+
// print bazelisk version information if "version" is the first non-flag argument
155155
// bazel version is executed after this command
156-
if len(args) > 0 && args[0] == "version" {
157-
// Check if the --gnu_format flag is set, if that is the case,
158-
// the version is printed differently
159-
var gnuFormat bool
160-
for _, arg := range args {
161-
if arg == "--gnu_format" {
162-
gnuFormat = true
163-
break
164-
}
165-
}
166-
156+
if ok, gnuFormat := isVersionCommand(args); ok {
167157
if gnuFormat {
168158
fmt.Printf("Bazelisk %s\n", BazeliskVersion)
169159
} else {
@@ -178,6 +168,24 @@ func RunBazeliskWithArgsFuncAndConfigAndOut(argsFunc ArgsFunc, repos *Repositori
178168
return exitCode, nil
179169
}
180170

171+
func isVersionCommand(args []string) (result bool, gnuFormat bool) {
172+
for _, arg := range args {
173+
// Check if the --gnu_format flag is set, if that is the case,
174+
// the version is printed differently
175+
if arg == "--gnu_format" {
176+
gnuFormat = true
177+
} else if arg == "version" {
178+
result = true
179+
} else if !strings.HasPrefix(arg, "--") {
180+
return // First non-flag arg is not "version" -> it must be a different command
181+
}
182+
if result && gnuFormat {
183+
break
184+
}
185+
}
186+
return
187+
}
188+
181189
// BazelInstallation provides a summary of a single install of `bazel`
182190
type BazelInstallation struct {
183191
Version string

0 commit comments

Comments
 (0)