From 7960f84743f0c291e6fe873c86472acc536e76d0 Mon Sep 17 00:00:00 2001 From: Bruno Meneguele Date: Sun, 28 Mar 2021 14:04:43 -0300 Subject: [PATCH] root: return to lab when git fails handling flags 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 --- cmd/root.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 0a897ff63..bd29a5ec8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 @@ -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 + } } }