Merge pull request #412 from skriss/cmd-changes
use cobra's arg-count validation & call Complete() before Validate()pull/415/head
commit
d1293825ef
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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]
|
||||
|
||||
|
|
Loading…
Reference in New Issue