Skip to content

Commit

Permalink
root: return to lab when git fails handling flags
Browse files Browse the repository at this point in the history
When `lab` doesn't recognize a flag to its root command, e.g. `lab --v`,
it passes to git in the hope the user wants it to passthrough. However,
when git also fails to handle the flag, it prints a failure message and
its usage message, which is quite confusing, since the user is issuing
`lab`.

This patch checks the return from git before printing anything and in
case it's a failure, let `lab` return its usage message instead of git's.

Signed-off-by: Bruno Meneguele <[email protected]>
  • Loading branch information
bmeneg committed Mar 28, 2021
1 parent 7ab2359 commit 7960f84
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ func Execute() {
return
}

// allow flags to the root cmd to be passed through. Technically we'll drop any exit code info which isn't ideal.
// allow flags to the root cmd to be passed through.
// Error msgs and codes generated by git are thrown away and the execution
// flow returns to lab to generate the error.
// TODO: remove for 1.0 when we stop wrapping git
if cmd.Use == RootCmd.Use && len(os.Args) > 1 {
var knownFlag bool
Expand All @@ -230,10 +232,19 @@ func Execute() {
knownFlag = true
}
}

// Pass unknown flags to git, if it also doesn't handle it, let lab
// handle the exit msg (help) and code.
if !knownFlag {
log.Println("Warning: lab's git passthrough command support will be removed in a later release.")
git.New(os.Args[1:]...).Run()
return
gitCmd := git.New(os.Args[1:]...)
gitCmd.Stderr = nil
gitCmd.Stdout = nil
out, err := gitCmd.CombinedOutput()
if err == nil {
fmt.Println(string(out))
return
}
}
}

Expand Down

0 comments on commit 7960f84

Please sign in to comment.