From d831a511f993f1d71e5581e4a78ceb25cb5a7add Mon Sep 17 00:00:00 2001 From: Kasey Date: Fri, 22 Jun 2018 17:37:41 -0400 Subject: [PATCH] feat(useOptions.validate): add `Validate` function to use command Did a bit of refactoring here. Previously, if the `qri use` command is run without any arguments, the cmd Run function (not the useOptions.Run function), would print out the cmd.Help() text. However, this breaks a few of our patterns. First, on no other command do we print the help text if no arguments are given Second, the Validate function would need to take a `*cobra.Command`. Since we are trying to modularize these functions (for clarity and for testing purposes), it seems like passing the Validate function the entire command violates the spirit of what we were doing in the last refactor. --- cmd/use.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/cmd/use.go b/cmd/use.go index 8af6fb058..9415f5a7e 100644 --- a/cmd/use.go +++ b/cmd/use.go @@ -28,10 +28,7 @@ func NewUseCommand(f Factory, ioStreams IOStreams) *cobra.Command { }, Run: func(cmd *cobra.Command, args []string) { ExitIfErr(o.ErrOut, o.Complete(f, args)) - if o.Clear == false && o.List == false && len(args) == 0 { - err := cmd.Help() - ExitIfErr(o.ErrOut, err) - } + ExitIfErr(o.ErrOut, o.Validate()) ExitIfErr(o.ErrOut, o.Run()) }, } @@ -60,6 +57,17 @@ func (o *UseOptions) Complete(f Factory, args []string) (err error) { return } +// Validate checks that any user input is valide +func (o *UseOptions) Validate() error { + if o.Clear == false && o.List == false && len(o.Refs) == 0 { + return lib.NewError(ErrBadArgs, "please provide dataset name, or --clear flag, or --list flag\nsee `qri use --help` for more info") + } + if o.Clear == true && o.List == true || o.Clear == true && len(o.Refs) != 0 || o.List == true && len(o.Refs) != 0 { + return lib.NewError(ErrBadArgs, "please only give a dataset name, or a --clear flag, or a --list flag") + } + return nil +} + // Run executes the search command func (o *UseOptions) Run() (err error) { var (