mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
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{
|
cmd := &cobra.Command{
|
||||||
Use: "volume",
|
Use: "volume",
|
||||||
Short: "Manage Docker volumes",
|
Short: "Manage Docker volumes",
|
||||||
// TODO: remove once cobra is patched to handle this
|
Args: cli.NoArgs,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Fprintf(dockerCli.Err(), "\n%s", cmd.UsageString())
|
fmt.Fprintf(dockerCli.Err(), "\n"+cmd.UsageString())
|
||||||
if len(args) > 0 {
|
|
||||||
return cli.StatusError{StatusCode: 1}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmd.AddCommand(
|
cmd.AddCommand(
|
||||||
|
|
|
@ -28,11 +28,8 @@ func newCreateCommand(dockerCli *client.DockerCli) *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "create",
|
Use: "create",
|
||||||
Short: "Create a volume",
|
Short: "Create a volume",
|
||||||
|
Args: cli.NoArgs,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
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)
|
return runCreate(dockerCli, opts)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,10 +20,8 @@ func newInspectCommand(dockerCli *client.DockerCli) *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "inspect [OPTIONS] VOLUME [VOLUME...]",
|
Use: "inspect [OPTIONS] VOLUME [VOLUME...]",
|
||||||
Short: "Return low-level information on a volume",
|
Short: "Return low-level information on a volume",
|
||||||
|
Args: cli.RequiresMinArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if err := cli.MinRequiredArgs(args, 1, cmd); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
opts.names = args
|
opts.names = args
|
||||||
return runInspect(dockerCli, opts)
|
return runInspect(dockerCli, opts)
|
||||||
},
|
},
|
||||||
|
|
|
@ -34,11 +34,8 @@ func newListCommand(dockerCli *client.DockerCli) *cobra.Command {
|
||||||
Use: "ls",
|
Use: "ls",
|
||||||
Aliases: []string{"list"},
|
Aliases: []string{"list"},
|
||||||
Short: "List volumes",
|
Short: "List volumes",
|
||||||
|
Args: cli.NoArgs,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
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)
|
return runList(dockerCli, opts)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,10 +15,8 @@ func newRemoveCommand(dockerCli *client.DockerCli) *cobra.Command {
|
||||||
Use: "rm VOLUME [VOLUME]...",
|
Use: "rm VOLUME [VOLUME]...",
|
||||||
Aliases: []string{"remove"},
|
Aliases: []string{"remove"},
|
||||||
Short: "Remove a volume",
|
Short: "Remove a volume",
|
||||||
|
Args: cli.RequiresMinArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if err := cli.MinRequiredArgs(args, 1, cmd); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return runRemove(dockerCli, args)
|
return runRemove(dockerCli, args)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -26,10 +24,11 @@ func newRemoveCommand(dockerCli *client.DockerCli) *cobra.Command {
|
||||||
|
|
||||||
func runRemove(dockerCli *client.DockerCli, volumes []string) error {
|
func runRemove(dockerCli *client.DockerCli, volumes []string) error {
|
||||||
client := dockerCli.Client()
|
client := dockerCli.Client()
|
||||||
var status = 0
|
ctx := context.Background()
|
||||||
|
status := 0
|
||||||
|
|
||||||
for _, name := range volumes {
|
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)
|
fmt.Fprintf(dockerCli.Err(), "%s\n", err)
|
||||||
status = 1
|
status = 1
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -2,30 +2,19 @@ package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MinRequiredArgs checks if the minimum number of args exists, and returns an
|
// NoArgs validate args and returns an error if there are any args
|
||||||
// error if they do not.
|
func NoArgs(cmd *cobra.Command, args []string) error {
|
||||||
func MinRequiredArgs(args []string, min int, cmd *cobra.Command) error {
|
if len(args) == 0 {
|
||||||
if len(args) >= min {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf(
|
if cmd.HasSubCommands() {
|
||||||
"\"%s\" requires at least %d argument(s).\n\nUsage: %s\n\n%s",
|
return fmt.Errorf("\n" + strings.TrimRight(cmd.UsageString(), "\n"))
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
|
@ -35,3 +24,19 @@ func AcceptsNoArgs(args []string, cmd *cobra.Command) error {
|
||||||
cmd.Short,
|
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)
|
return fmt.Errorf("Should not have full usage on %q\n", args)
|
||||||
}
|
}
|
||||||
if strings.HasSuffix(stderr, "\n\n") {
|
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…
Add table
Reference in a new issue