Use Args in cobra.Command to validate args.
Also re-use context. Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
54b5cce7e4
commit
fc5a4514fb
7 changed files with 33 additions and 41 deletions
|
@ -14,13 +14,9 @@ func NewVolumeCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|||
cmd := &cobra.Command{
|
||||
Use: "volume",
|
||||
Short: "Manage Docker volumes",
|
||||
// TODO: remove once cobra is patched to handle this
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
fmt.Fprintf(dockerCli.Err(), "\n%s", cmd.UsageString())
|
||||
if len(args) > 0 {
|
||||
return cli.StatusError{StatusCode: 1}
|
||||
}
|
||||
return nil
|
||||
Args: cli.NoArgs,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Fprintf(dockerCli.Err(), "\n"+cmd.UsageString())
|
||||
},
|
||||
}
|
||||
cmd.AddCommand(
|
||||
|
|
|
@ -28,11 +28,8 @@ func newCreateCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|||
cmd := &cobra.Command{
|
||||
Use: "create",
|
||||
Short: "Create a volume",
|
||||
Args: cli.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
// TODO: remove once cobra is patched to handle this
|
||||
if err := cli.AcceptsNoArgs(args, cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
return runCreate(dockerCli, opts)
|
||||
},
|
||||
}
|
||||
|
|
|
@ -20,10 +20,8 @@ func newInspectCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|||
cmd := &cobra.Command{
|
||||
Use: "inspect [OPTIONS] VOLUME [VOLUME...]",
|
||||
Short: "Return low-level information on a volume",
|
||||
Args: cli.RequiresMinArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := cli.MinRequiredArgs(args, 1, cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
opts.names = args
|
||||
return runInspect(dockerCli, opts)
|
||||
},
|
||||
|
|
|
@ -34,11 +34,8 @@ func newListCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|||
Use: "ls",
|
||||
Aliases: []string{"list"},
|
||||
Short: "List volumes",
|
||||
Args: cli.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
// TODO: remove once cobra is patched to handle this
|
||||
if err := cli.AcceptsNoArgs(args, cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
return runList(dockerCli, opts)
|
||||
},
|
||||
}
|
||||
|
|
|
@ -15,10 +15,8 @@ func newRemoveCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|||
Use: "rm VOLUME [VOLUME]...",
|
||||
Aliases: []string{"remove"},
|
||||
Short: "Remove a volume",
|
||||
Args: cli.RequiresMinArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := cli.MinRequiredArgs(args, 1, cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
return runRemove(dockerCli, args)
|
||||
},
|
||||
}
|
||||
|
@ -26,10 +24,11 @@ func newRemoveCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|||
|
||||
func runRemove(dockerCli *client.DockerCli, volumes []string) error {
|
||||
client := dockerCli.Client()
|
||||
var status = 0
|
||||
ctx := context.Background()
|
||||
status := 0
|
||||
|
||||
for _, name := range volumes {
|
||||
if err := client.VolumeRemove(context.Background(), name); err != nil {
|
||||
if err := client.VolumeRemove(ctx, name); err != nil {
|
||||
fmt.Fprintf(dockerCli.Err(), "%s\n", err)
|
||||
status = 1
|
||||
continue
|
||||
|
|
|
@ -2,30 +2,19 @@ package cli
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// MinRequiredArgs checks if the minimum number of args exists, and returns an
|
||||
// error if they do not.
|
||||
func MinRequiredArgs(args []string, min int, cmd *cobra.Command) error {
|
||||
if len(args) >= min {
|
||||
// NoArgs validate args and returns an error if there are any args
|
||||
func NoArgs(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf(
|
||||
"\"%s\" requires at least %d argument(s).\n\nUsage: %s\n\n%s",
|
||||
cmd.CommandPath(),
|
||||
min,
|
||||
cmd.UseLine(),
|
||||
cmd.Short,
|
||||
)
|
||||
}
|
||||
|
||||
// AcceptsNoArgs returns an error message if there are args
|
||||
func AcceptsNoArgs(args []string, cmd *cobra.Command) error {
|
||||
if len(args) == 0 {
|
||||
return nil
|
||||
if cmd.HasSubCommands() {
|
||||
return fmt.Errorf("\n" + strings.TrimRight(cmd.UsageString(), "\n"))
|
||||
}
|
||||
|
||||
return fmt.Errorf(
|
||||
|
@ -35,3 +24,19 @@ func AcceptsNoArgs(args []string, cmd *cobra.Command) error {
|
|||
cmd.Short,
|
||||
)
|
||||
}
|
||||
|
||||
// RequiresMinArgs returns an error if there is not at least min args
|
||||
func RequiresMinArgs(min int) cobra.PositionalArgs {
|
||||
return func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) >= min {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf(
|
||||
"\"%s\" requires at least %d argument(s).\n\nUsage: %s\n\n%s",
|
||||
cmd.CommandPath(),
|
||||
min,
|
||||
cmd.UseLine(),
|
||||
cmd.Short,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -335,7 +335,7 @@ func testCommand(cmd string, newEnvs []string, scanForHome bool, home string) er
|
|||
return fmt.Errorf("Should not have full usage on %q\n", args)
|
||||
}
|
||||
if strings.HasSuffix(stderr, "\n\n") {
|
||||
return fmt.Errorf("Should not have a blank line on %q\n", args)
|
||||
return fmt.Errorf("Should not have a blank line on %q\n%v", args, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue