diff --git a/cmd/add.go b/cmd/add.go index 151da7637..ff19d35f7 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -44,9 +44,14 @@ changes to qri.`, create a dataset with a dataset data file: $ qri add --file dataset.yaml --body comics.csv me/comic_characters`, - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f)) - ExitIfErr(o.ErrOut, o.Run(args)) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f); err != nil { + return err + } + if err := o.Run(args); err != nil { + return err + } + return nil }, } diff --git a/cmd/body.go b/cmd/body.go index 00fc6ac59..c98e60e90 100644 --- a/cmd/body.go +++ b/cmd/body.go @@ -26,9 +26,14 @@ body reads records from a dataset`, "group": "dataset", }, Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Run()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil }, } diff --git a/cmd/cmd.go b/cmd/cmd.go index 6e0799303..b964c8382 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -33,9 +33,15 @@ func Execute() { } root := NewQriCommand(EnvPathFactory, os.Stdin, os.Stdout, os.Stderr) + // If the subcommand hits an error, don't show usage or the error, since we'll show + // the error message below, on our own. Usage is still shown if the subcommand + // is missing command-line arguments. + root.SilenceUsage = true + root.SilenceErrors = true + // Execute the subcommand if err := root.Execute(); err != nil { printErr(os.Stdin, err) - os.Exit(-1) + os.Exit(1) } } diff --git a/cmd/config.go b/cmd/config.go index 91838cd06..912165412 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -52,18 +52,29 @@ The --with-private-keys option will show private keys. PLEASE PLEASE PLEASE NEVER SHARE YOUR PRIVATE KEYS WITH ANYONE. EVER. Anyone with your private keys can impersonate you on qri.`, Args: cobra.MaximumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f)) - ExitIfErr(o.ErrOut, o.Get(args)) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f); err != nil { + return err + } + if err := o.Get(args); err != nil { + return err + } + return nil }, } set := &cobra.Command{ Use: "set", Short: "Set a configuration option", - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f)) - ExitIfErr(o.ErrOut, o.Set(args)) + RunE: func(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true + if err := o.Complete(f); err != nil { + return err + } + if err := o.Set(args); err != nil { + return err + } + return nil }, } diff --git a/cmd/connect.go b/cmd/connect.go index 5e3f0cad0..3b6e4ae64 100644 --- a/cmd/connect.go +++ b/cmd/connect.go @@ -30,9 +30,14 @@ things: When you run connect you are connecting to the distributed web, interacting with peers & swapping data.`, - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Run()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil }, } diff --git a/cmd/diff.go b/cmd/diff.go index 4aa8b4ec5..063a0d190 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -27,9 +27,14 @@ either by name or by their hash`, "group": "dataset", }, Args: cobra.MinimumNArgs(2), - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Run()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil }, } diff --git a/cmd/export.go b/cmd/export.go index c08d9bf77..8fff2cbf0 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -35,9 +35,14 @@ To export everything about a dataset, use the --dataset flag.`, Annotations: map[string]string{ "group": "dataset", }, - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Run()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil }, } diff --git a/cmd/get.go b/cmd/get.go index 930709918..7fde6dae4 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -18,9 +18,14 @@ func NewGetCommand(f Factory, ioStreams IOStreams) *cobra.Command { Annotations: map[string]string{ "group": "dataset", }, - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Run()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil }, } diff --git a/cmd/info.go b/cmd/info.go index 10383d33a..85ebe618e 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -31,9 +31,14 @@ func NewInfoCommand(f Factory, ioStreams IOStreams) *cobra.Command { "group": "dataset", }, Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Run()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil }, } diff --git a/cmd/list.go b/cmd/list.go index f8503c143..5b3f9f3da 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -27,9 +27,14 @@ qri repository.`, Annotations: map[string]string{ "group": "dataset", }, - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Run()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil }, } diff --git a/cmd/log.go b/cmd/log.go index a42c34a73..fbcf1e5be 100644 --- a/cmd/log.go +++ b/cmd/log.go @@ -30,9 +30,14 @@ working backwards in time.`, "group": "dataset", }, Args: cobra.ExactArgs(1), - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Run()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil }, } diff --git a/cmd/peers.go b/cmd/peers.go index f0f9768ea..f93e132a2 100644 --- a/cmd/peers.go +++ b/cmd/peers.go @@ -33,9 +33,14 @@ format is yaml.`, show info in json: $ qri peers info b5 --format json`, Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Info()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Info(); err != nil { + return err + } + return nil }, } @@ -54,9 +59,14 @@ command in the background or in another terminal window. to ensure you get a cached version of the list: $ qri peers list --cached`, Aliases: []string{"ls"}, - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.List()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.List(); err != nil { + return err + } + return nil }, } @@ -64,9 +74,14 @@ command in the background or in another terminal window. Use: "connect", Short: "connect to a peer", Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Connect()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Connect(); err != nil { + return err + } + return nil }, } @@ -74,9 +89,14 @@ command in the background or in another terminal window. Use: "disconnect", Short: "explicitly close a connection to a peer", Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Disconnect()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Disconnect(); err != nil { + return err + } + return nil }, } diff --git a/cmd/registry.go b/cmd/registry.go index c5d9bb238..ea767e7ef 100644 --- a/cmd/registry.go +++ b/cmd/registry.go @@ -48,9 +48,14 @@ $ qri config set registry.location ""`, Example: ` Publish a dataset you've created to the registry: $ qri registry publish me/dataset_name`, Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Publish()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Publish(); err != nil { + return err + } + return nil }, } @@ -61,9 +66,14 @@ $ qri config set registry.location ""`, Example: ` Remove a dataset from the registry: $ qri registry unpublish me/dataset_name`, Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Unpublish()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Unpublish(); err != nil { + return err + } + return nil }, } diff --git a/cmd/remove.go b/cmd/remove.go index 5051a0c3c..049ac3e4d 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -32,9 +32,14 @@ both qri & IPFS. Promise.`, Annotations: map[string]string{ "group": "dataset", }, - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Run()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil }, } diff --git a/cmd/rename.go b/cmd/rename.go index eabea5508..ccff14547 100644 --- a/cmd/rename.go +++ b/cmd/rename.go @@ -26,9 +26,14 @@ renames to a minimum.`, Annotations: map[string]string{ "group": "dataset", }, - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Run()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil }, } diff --git a/cmd/render.go b/cmd/render.go index b160950fb..323ee1497 100644 --- a/cmd/render.go +++ b/cmd/render.go @@ -24,10 +24,17 @@ func NewRenderCommand(f Factory, ioStreams IOStreams) *cobra.Command { Annotations: map[string]string{ "group": "dataset", }, - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Validate()) - ExitIfErr(o.ErrOut, o.Run()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Validate(); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil }, } diff --git a/cmd/save.go b/cmd/save.go index 81c07c541..22db10610 100644 --- a/cmd/save.go +++ b/cmd/save.go @@ -41,10 +41,17 @@ collaboration are in the works. Sit tight sportsfans.`, Annotations: map[string]string{ "group": "dataset", }, - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Validate()) - ExitIfErr(o.ErrOut, o.Run()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Validate(); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil }, } diff --git a/cmd/search.go b/cmd/search.go index 0f87f306d..fa8a5e1cd 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -19,10 +19,17 @@ func NewSearchCommand(f Factory, ioStreams IOStreams) *cobra.Command { Annotations: map[string]string{ "group": "network", }, - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Validate()) - ExitIfErr(o.ErrOut, o.Run()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Validate(); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil }, } diff --git a/cmd/setup.go b/cmd/setup.go index 0e1f73647..b54ccad8f 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -37,9 +37,14 @@ overwrite this info.`, Annotations: map[string]string{ "group": "other", }, - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Run(f)) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Run(f); err != nil { + return err + } + return nil }, } diff --git a/cmd/use.go b/cmd/use.go index 9415f5a7e..b2e5d5e4c 100644 --- a/cmd/use.go +++ b/cmd/use.go @@ -26,10 +26,17 @@ func NewUseCommand(f Factory, ioStreams IOStreams) *cobra.Command { Annotations: map[string]string{ "group": "dataset", }, - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Validate()) - ExitIfErr(o.ErrOut, o.Run()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Validate(); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil }, } diff --git a/cmd/validate.go b/cmd/validate.go index 0e47fabc0..b41b43918 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -51,10 +51,17 @@ will affect a dataset before saving changes to a dataset. Note: --body and --schema flags will override the dataset if both flags are provided.`, Example: ` show errors in an existing dataset: $ qri validate b5/comics`, - Run: func(cmd *cobra.Command, args []string) { - ExitIfErr(o.ErrOut, o.Complete(f, args)) - ExitIfErr(o.ErrOut, o.Validate()) - ExitIfErr(o.ErrOut, o.Run()) + RunE: func(cmd *cobra.Command, args []string) error { + if err := o.Complete(f, args); err != nil { + return err + } + if err := o.Validate(); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil }, } diff --git a/cmd/version.go b/cmd/version.go index 21265bcd5..d9f9f110a 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -15,8 +15,9 @@ For updates & further information check https://github.com/qri-io/qri/releases`, Annotations: map[string]string{ "group": "other", }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { printInfo(ioStreams.Out, lib.VersionNumber) + return nil }, } return cmd