Skip to content

Commit

Permalink
feat(useOptions.validate): add Validate function to use command
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ramfox committed Jun 28, 2018
1 parent e1d6110 commit d831a51
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit d831a51

Please sign in to comment.