use cobra's arg-count validation & call Complete() before Validate()

Signed-off-by: Steve Kriss <steve@heptio.com>
This commit is contained in:
Steve Kriss
2018-04-03 09:45:29 -07:00
parent a2f5e14a32
commit c60e47dedd
13 changed files with 18 additions and 67 deletions
+2 -6
View File
@@ -20,7 +20,6 @@ import (
"fmt"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
@@ -39,9 +38,10 @@ func NewCreateCommand(f client.Factory, use string) *cobra.Command {
c := &cobra.Command{
Use: use + " NAME",
Short: "Create a backup",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
cmd.CheckError(o.Validate(c, args))
cmd.CheckError(o.Complete(args))
cmd.CheckError(o.Validate(c, args))
cmd.CheckError(o.Run(c, f))
},
}
@@ -94,10 +94,6 @@ func (o *CreateOptions) BindFlags(flags *pflag.FlagSet) {
}
func (o *CreateOptions) Validate(c *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("you must specify only one argument, the backup's name")
}
if err := output.ValidateFlags(c); err != nil {
return err
}
+2 -5
View File
@@ -45,9 +45,10 @@ func NewDeleteCommand(f client.Factory, use string) *cobra.Command {
c := &cobra.Command{
Use: fmt.Sprintf("%s NAME", use),
Short: "Delete a backup",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
cmd.CheckError(o.Validate(c, args, f))
cmd.CheckError(o.Complete(f, args))
cmd.CheckError(o.Validate(c, args, f))
cmd.CheckError(o.Run())
},
}
@@ -72,10 +73,6 @@ func (o *DeleteOptions) BindFlags(flags *pflag.FlagSet) {
}
func (o *DeleteOptions) Validate(c *cobra.Command, args []string, f client.Factory) error {
if len(args) != 1 {
return errors.New("you must specify only one argument, the backup's name")
}
kubeClient, err := f.KubeClient()
if err != nil {
return err
+2 -5
View File
@@ -37,9 +37,10 @@ func NewDownloadCommand(f client.Factory) *cobra.Command {
c := &cobra.Command{
Use: "download NAME",
Short: "Download a backup",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
cmd.CheckError(o.Validate(c, args))
cmd.CheckError(o.Complete(args))
cmd.CheckError(o.Validate(c, args))
cmd.CheckError(o.Run(c, f))
},
}
@@ -70,10 +71,6 @@ func (o *DownloadOptions) BindFlags(flags *pflag.FlagSet) {
}
func (o *DownloadOptions) Validate(c *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("backup name is required")
}
return nil
}
+1 -6
View File
@@ -20,7 +20,6 @@ import (
"os"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/heptio/ark/pkg/apis/ark/v1"
@@ -35,12 +34,8 @@ func NewLogsCommand(f client.Factory) *cobra.Command {
c := &cobra.Command{
Use: "logs BACKUP",
Short: "Get backup logs",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
if len(args) != 1 {
err := errors.New("backup name is required")
cmd.CheckError(err)
}
arkClient, err := f.Client()
cmd.CheckError(err)