Skip to content

Commit

Permalink
mixer: avoid extra output when mixer fails
Browse files Browse the repository at this point in the history
Disable automatic print of errors and usage information from Cobra,
and only print usage information if it was a command / parsing flag
issue.

This also fixes the duplicate errors in current code (both our code
and cobra ware printing errors).

Signed-off-by: Caio Marcelo de Oliveira Filho <[email protected]>
  • Loading branch information
cmarcelo committed Jan 4, 2018
1 parent 12c320e commit ed5559c
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions mixer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os"
"os/exec"
"strconv"
"strings"

"github.com/clearlinux/mixer-tools/builder"

Expand All @@ -31,6 +32,9 @@ var config string
var RootCmd = &cobra.Command{
Use: "mixer",
Long: `Mixer is a tool used to compose OS update content and images.`,

SilenceUsage: true,
SilenceErrors: true,
}

type initCmdFlags struct {
Expand All @@ -54,11 +58,33 @@ var initCmd = &cobra.Command{
},
}

var parseErrorPrefixes = []string{
"unknown command \"",
"unknown flag:",
"unknown shorthand flag:",
"invalid argument \"",
"flag needs an argument:",
"bad flag syntax:",
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Mixer Error: %s\n", err)
cmd, err := RootCmd.ExecuteC()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)

// Automatically show usage information if it is a flag parsing error. Flag parsing
// doesn't return errors that we can use types or interfaces to match, so do an
// approximation.
s := err.Error()
for _, p := range parseErrorPrefixes {
if strings.HasPrefix(s, p) {
cmd.Println()
cmd.Print(cmd.UsageString())
break
}
}
os.Exit(1)
}
}
Expand Down

0 comments on commit ed5559c

Please sign in to comment.