Merge pull request #412 from skriss/cmd-changes

use cobra's arg-count validation & call Complete() before Validate()
pull/415/head
Andy Goldstein 2018-04-04 16:39:48 -04:00 committed by GitHub
commit d1293825ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 37 additions and 82 deletions

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
}

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

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
}

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)

View File

@ -23,7 +23,6 @@ import (
"github.com/heptio/ark/pkg/client"
"github.com/heptio/ark/pkg/cmd"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -31,11 +30,8 @@ func NewSetCommand() *cobra.Command {
c := &cobra.Command{
Use: "set KEY=VALUE [KEY=VALUE]...",
Short: "Set client configuration file values",
Args: cobra.MinimumNArgs(1),
Run: func(c *cobra.Command, args []string) {
if len(args) < 1 {
cmd.CheckError(errors.Errorf("At least one KEY=VALUE argument is required"))
}
config, err := client.LoadConfig()
cmd.CheckError(err)

View File

@ -50,11 +50,8 @@ func NewAddCommand(f client.Factory) *cobra.Command {
c := &cobra.Command{
Use: "add IMAGE",
Short: "Add a plugin",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
if len(args) != 1 {
cmd.CheckError(errors.New("you must specify only one argument, the plugin container image"))
}
kubeClient, err := f.KubeClient()
if err != nil {
cmd.CheckError(err)

View File

@ -35,11 +35,8 @@ func NewRemoveCommand(f client.Factory) *cobra.Command {
c := &cobra.Command{
Use: "remove [NAME | IMAGE]",
Short: "Remove a plugin",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
if len(args) != 1 {
cmd.CheckError(errors.New("you must specify only one argument, the plugin container's name or image"))
}
kubeClient, err := f.KubeClient()
if err != nil {
cmd.CheckError(err)

View File

@ -45,9 +45,10 @@ func NewCreateCommand(f client.Factory, use string) *cobra.Command {
# create a restore with a default name ("backup-1-<timestamp>") from backup "backup-1"
ark restore create --from-backup backup-1`,
Args: cobra.MaximumNArgs(1),
Run: func(c *cobra.Command, args []string) {
cmd.CheckError(o.Complete(args, f))
cmd.CheckError(o.Validate(c, args, f))
cmd.CheckError(o.Complete(args))
cmd.CheckError(o.Run(c, f))
},
}
@ -108,35 +109,35 @@ func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Facto
return errors.New("--from-backup is required")
}
if len(args) > 1 {
return errors.New("you may specify at most one argument, the restore's name")
}
if err := output.ValidateFlags(c); err != nil {
return err
}
if o.client == nil {
// This should never happen
return errors.New("Ark client is not set; unable to proceed")
}
if _, err := o.client.ArkV1().Backups(f.Namespace()).Get(o.BackupName, metav1.GetOptions{}); err != nil {
return err
}
return nil
}
func (o *CreateOptions) Complete(args []string, f client.Factory) error {
if len(args) == 1 {
o.RestoreName = args[0]
} else {
o.RestoreName = fmt.Sprintf("%s-%s", o.BackupName, time.Now().Format("20060102150405"))
}
client, err := f.Client()
if err != nil {
return err
}
o.client = client
_, err = o.client.ArkV1().Backups(f.Namespace()).Get(o.BackupName, metav1.GetOptions{})
if err != nil {
return err
}
return nil
}
func (o *CreateOptions) Complete(args []string) error {
if len(args) == 1 {
o.RestoreName = args[0]
} else {
o.RestoreName = fmt.Sprintf("%s-%s", o.BackupName, time.Now().Format("20060102150405"))
}
return nil
}

View File

@ -18,7 +18,6 @@ package restore
import (
"fmt"
"os"
"github.com/spf13/cobra"
@ -30,12 +29,8 @@ func NewDeleteCommand(f client.Factory, use string) *cobra.Command {
c := &cobra.Command{
Use: fmt.Sprintf("%s NAME", use),
Short: "Delete a restore",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
if len(args) != 1 {
c.Usage()
os.Exit(1)
}
arkClient, err := f.Client()
cmd.CheckError(err)

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 RESTORE",
Short: "Get restore logs",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
if len(args) != 1 {
err := errors.New("restore name is required")
cmd.CheckError(err)
}
arkClient, err := f.Client()
cmd.CheckError(err)

View File

@ -49,10 +49,10 @@ func NewCreateCommand(f client.Factory, use string) *cobra.Command {
| 5 | Day of Week | 0-7,* |`,
Example: `ark create schedule NAME --schedule="0 */6 * * *"`,
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))
},
}
@ -83,9 +83,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 schedule's name")
}
if len(o.Schedule) == 0 {
return errors.New("--schedule is required")
}

View File

@ -18,7 +18,6 @@ package schedule
import (
"fmt"
"os"
"github.com/spf13/cobra"
@ -30,12 +29,8 @@ func NewDeleteCommand(f client.Factory, use string) *cobra.Command {
c := &cobra.Command{
Use: fmt.Sprintf("%s NAME", use),
Short: "Delete a schedule",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
if len(args) != 1 {
c.Usage()
os.Exit(1)
}
arkClient, err := f.Client()
cmd.CheckError(err)

View File

@ -60,11 +60,8 @@ func NewCommand() *cobra.Command {
Use: "run-plugin [KIND] [NAME]",
Hidden: true,
Short: "INTERNAL COMMAND ONLY - not intended to be run directly by users",
Args: cobra.ExactArgs(2),
Run: func(c *cobra.Command, args []string) {
if len(args) != 2 {
logger.Fatal("You must specify exactly two arguments, the plugin kind and the plugin name")
}
kind := args[0]
name := args[1]