Skip to content

Commit

Permalink
mixer: reduce noise when commands fail
Browse files Browse the repository at this point in the history
Only print errors once, not twice. Do not print usage information
unless the command was called with invalid arguments.

Because cobra already print its own errors, don't print them again in
Execute function, just ensure we get the right exit code. Mixer errors
will be handled by our code, instead of passing the errors to cobra
and then later get them back. That way no usage is printed.

Two helper functions were added fail/failf to perform Print+Exit.

Signed-off-by: Caio Marcelo de Oliveira Filho <caio.oliveira@intel.com>
  • Loading branch information
cmarcelo authored and tmarcu committed Jan 9, 2018
1 parent 299138d commit ebf6a40
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
26 changes: 13 additions & 13 deletions mixer/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,68 +72,68 @@ var buildChrootsCmd = &cobra.Command{
Use: "chroots",
Short: "Build the chroots for your mix",
Long: `Build the chroots for your mix`,
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
b := builder.NewFromConfig(config)
return buildChroots(b, buildFlags.noSigning)
err := buildChroots(b, buildFlags.noSigning)
if err != nil {
fail(err)
}
},
}

var buildUpdateCmd = &cobra.Command{
Use: "update",
Short: "Build the update content for your mix",
Long: `Build the update content for your mix`,
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
b := builder.NewFromConfig(config)
err := b.BuildUpdate(buildFlags.prefix, buildFlags.minVersion, buildFlags.format, buildFlags.noSigning, !buildFlags.noPublish, buildFlags.keepChroot)
if err != nil {
return errors.Wrap(err, "couldn't build update")
failf("couldn't build update: %s", err)
}

if buildFlags.increment {
b.UpdateMixVer()
}
return nil
},
}

var buildAllCmd = &cobra.Command{
Use: "all",
Short: "Build all content for mix with default options",
Long: `Build all content for mix with default options`,
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
b := builder.NewFromConfig(config)
rpms, err := ioutil.ReadDir(b.RPMdir)
if err == nil {
err = b.AddRPMList(rpms)
if err != nil {
return errors.Wrap(err, "couldn't add the RPMs")
failf("couldn't add the RPMs: %s", err)
}
}
err = buildChroots(b, buildFlags.noSigning)
if err != nil {
return errors.Wrap(err, "Error building chroots")
failf("couldn't build chroots: %s", err)
}
err = b.BuildUpdate(buildFlags.prefix, buildFlags.minVersion, buildFlags.format, buildFlags.noSigning, !buildFlags.noPublish, buildFlags.keepChroot)
if err != nil {
return errors.Wrap(err, "Error building update")
failf("couldn't build update: %s", err)
}

b.UpdateMixVer()
return nil
},
}

var buildImageCmd = &cobra.Command{
Use: "image",
Short: "Build an image from the mix content",
Long: `Build an image from the mix content`,
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
b := builder.NewFromConfig(config)
err := b.BuildImage(buildFlags.format, buildFlags.template)
if err != nil {
return errors.Wrap(err, "Error building image")
failf("couldn't build image: %s", err)
}
return nil
},
}

Expand Down
9 changes: 3 additions & 6 deletions mixer/cmd/bundles.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

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

"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -41,31 +40,29 @@ var addBundlesCmd = &cobra.Command{
Use: "add [bundle(s)]",
Short: "Add clr-bundles to your mix",
Long: `Add clr-bundles to your mix`,
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
if bundleFlags.all == false {
if len(args) <= 0 {
return errors.New("bundle add requires at least 1 argument if --all is not passed")
failf("bundle add requires at least 1 argument if --all is not passed")
}
}
bundles := strings.Split(args[0], ",")
b := builder.NewFromConfig(config)
// TODO change this to return (int, error)
numadded := b.AddBundles(bundles, bundleFlags.force, bundleFlags.all, bundleFlags.git)
fmt.Println(numadded, " bundles were added")
return nil
},
}

var getBundlesCmd = &cobra.Command{
Use: "get",
Short: "Get the clr-bundles from upstream",
Long: `Get the clr-bundles from upstream`,
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
b := builder.NewFromConfig(config)
fmt.Println("Getting clr-bundles for version " + b.Clearver)
// TODO change this to return an error
b.UpdateRepo(b.Clearver, false)
return nil
},
}

Expand Down
18 changes: 15 additions & 3 deletions mixer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,21 @@ var initCmd = &cobra.Command{
Use: "init-mix",
Short: "Initialize the mixer and workspace",
Long: `Initialize the mixer and workspace`,
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
b := builder.New()
b.LoadBuilderConf(config)
b.ReadBuilderConf()
return b.InitMix(strconv.Itoa(initFlags.clearver), strconv.Itoa(initFlags.mixver), initFlags.all, initFlags.upstreamurl)
err := b.InitMix(strconv.Itoa(initFlags.clearver), strconv.Itoa(initFlags.mixver), initFlags.all, initFlags.upstreamurl)
if err != nil {
fail(err)
}
},
}

// 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)
os.Exit(1)
}
}
Expand Down Expand Up @@ -96,3 +98,13 @@ func init() {
initCmd.Flags().StringVar(&config, "config", "", "Supply a specific builder.conf to use for mixing")
initCmd.Flags().StringVar(&initFlags.upstreamurl, "upstream-url", "https://download.clearlinux.org", "Supply an upstream URL to use for mixing")
}

func fail(err error) {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}

func failf(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, fmt.Sprintf("ERROR: %s\n", format), a...)
os.Exit(1)
}
14 changes: 8 additions & 6 deletions mixer/cmd/rpms.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ import (

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

"github.com/pkg/errors"
"github.com/spf13/cobra"
)

var addRPMCmd = &cobra.Command{
Use: "add-rpms",
Short: "Add RPMs to local yum repository",
Long: `Add RPMS from the configured RPMDIR to local yum repository`,
RunE: runAddRPM,
Run: runAddRPM,
}

var rpmCmds = []*cobra.Command{
Expand All @@ -41,14 +40,17 @@ func init() {
}
}

func runAddRPM(cmd *cobra.Command, args []string) error {
func runAddRPM(cmd *cobra.Command, args []string) {
b := builder.NewFromConfig(config)
if b.RPMdir == "" {
return errors.Errorf("RPMDIR not set in configuration")
failf("RPMDIR not set in configuration")
}
rpms, err := ioutil.ReadDir(b.RPMdir)
if err != nil {
return errors.Wrapf(err, "cannot read RPMDIR")
failf("cannot read RPMDIR: %s", err)
}
err = b.AddRPMList(rpms)
if err != nil {
fail(err)
}
return b.AddRPMList(rpms)
}

0 comments on commit ebf6a40

Please sign in to comment.