Skip to content

Commit

Permalink
Print Bazelisk version when startup flags are set. (#646)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
fweikert authored Dec 10, 2024
1 parent 86ef884 commit 8eec345
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 8eec345

Please sign in to comment.