Browse code

Use Args in cobra.Command to validate args.

Also re-use context.

Signed-off-by: Daniel Nephin <dnephin@docker.com>

Daniel Nephin authored on 2016/05/27 06:57:31
Showing 7 changed files
... ...
@@ -14,13 +14,9 @@ func NewVolumeCommand(dockerCli *client.DockerCli) *cobra.Command {
14 14
 	cmd := &cobra.Command{
15 15
 		Use:   "volume",
16 16
 		Short: "Manage Docker volumes",
17
-		// TODO: remove once cobra is patched to handle this
18
-		RunE: func(cmd *cobra.Command, args []string) error {
19
-			fmt.Fprintf(dockerCli.Err(), "\n%s", cmd.UsageString())
20
-			if len(args) > 0 {
21
-				return cli.StatusError{StatusCode: 1}
22
-			}
23
-			return nil
17
+		Args:  cli.NoArgs,
18
+		Run: func(cmd *cobra.Command, args []string) {
19
+			fmt.Fprintf(dockerCli.Err(), "\n"+cmd.UsageString())
24 20
 		},
25 21
 	}
26 22
 	cmd.AddCommand(
... ...
@@ -28,11 +28,8 @@ func newCreateCommand(dockerCli *client.DockerCli) *cobra.Command {
28 28
 	cmd := &cobra.Command{
29 29
 		Use:   "create",
30 30
 		Short: "Create a volume",
31
+		Args:  cli.NoArgs,
31 32
 		RunE: func(cmd *cobra.Command, args []string) error {
32
-			// TODO: remove once cobra is patched to handle this
33
-			if err := cli.AcceptsNoArgs(args, cmd); err != nil {
34
-				return err
35
-			}
36 33
 			return runCreate(dockerCli, opts)
37 34
 		},
38 35
 	}
... ...
@@ -20,10 +20,8 @@ func newInspectCommand(dockerCli *client.DockerCli) *cobra.Command {
20 20
 	cmd := &cobra.Command{
21 21
 		Use:   "inspect [OPTIONS] VOLUME [VOLUME...]",
22 22
 		Short: "Return low-level information on a volume",
23
+		Args:  cli.RequiresMinArgs(1),
23 24
 		RunE: func(cmd *cobra.Command, args []string) error {
24
-			if err := cli.MinRequiredArgs(args, 1, cmd); err != nil {
25
-				return err
26
-			}
27 25
 			opts.names = args
28 26
 			return runInspect(dockerCli, opts)
29 27
 		},
... ...
@@ -34,11 +34,8 @@ func newListCommand(dockerCli *client.DockerCli) *cobra.Command {
34 34
 		Use:     "ls",
35 35
 		Aliases: []string{"list"},
36 36
 		Short:   "List volumes",
37
+		Args:    cli.NoArgs,
37 38
 		RunE: func(cmd *cobra.Command, args []string) error {
38
-			// TODO: remove once cobra is patched to handle this
39
-			if err := cli.AcceptsNoArgs(args, cmd); err != nil {
40
-				return err
41
-			}
42 39
 			return runList(dockerCli, opts)
43 40
 		},
44 41
 	}
... ...
@@ -15,10 +15,8 @@ func newRemoveCommand(dockerCli *client.DockerCli) *cobra.Command {
15 15
 		Use:     "rm VOLUME [VOLUME]...",
16 16
 		Aliases: []string{"remove"},
17 17
 		Short:   "Remove a volume",
18
+		Args:    cli.RequiresMinArgs(1),
18 19
 		RunE: func(cmd *cobra.Command, args []string) error {
19
-			if err := cli.MinRequiredArgs(args, 1, cmd); err != nil {
20
-				return err
21
-			}
22 20
 			return runRemove(dockerCli, args)
23 21
 		},
24 22
 	}
... ...
@@ -26,10 +24,11 @@ func newRemoveCommand(dockerCli *client.DockerCli) *cobra.Command {
26 26
 
27 27
 func runRemove(dockerCli *client.DockerCli, volumes []string) error {
28 28
 	client := dockerCli.Client()
29
-	var status = 0
29
+	ctx := context.Background()
30
+	status := 0
30 31
 
31 32
 	for _, name := range volumes {
32
-		if err := client.VolumeRemove(context.Background(), name); err != nil {
33
+		if err := client.VolumeRemove(ctx, name); err != nil {
33 34
 			fmt.Fprintf(dockerCli.Err(), "%s\n", err)
34 35
 			status = 1
35 36
 			continue
... ...
@@ -2,30 +2,19 @@ package cli
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"strings"
5 6
 
6 7
 	"github.com/spf13/cobra"
7 8
 )
8 9
 
9
-// MinRequiredArgs checks if the minimum number of args exists, and returns an
10
-// error if they do not.
11
-func MinRequiredArgs(args []string, min int, cmd *cobra.Command) error {
12
-	if len(args) >= min {
10
+// NoArgs validate args and returns an error if there are any args
11
+func NoArgs(cmd *cobra.Command, args []string) error {
12
+	if len(args) == 0 {
13 13
 		return nil
14 14
 	}
15 15
 
16
-	return fmt.Errorf(
17
-		"\"%s\" requires at least %d argument(s).\n\nUsage:  %s\n\n%s",
18
-		cmd.CommandPath(),
19
-		min,
20
-		cmd.UseLine(),
21
-		cmd.Short,
22
-	)
23
-}
24
-
25
-// AcceptsNoArgs returns an error message if there are args
26
-func AcceptsNoArgs(args []string, cmd *cobra.Command) error {
27
-	if len(args) == 0 {
28
-		return nil
16
+	if cmd.HasSubCommands() {
17
+		return fmt.Errorf("\n" + strings.TrimRight(cmd.UsageString(), "\n"))
29 18
 	}
30 19
 
31 20
 	return fmt.Errorf(
... ...
@@ -35,3 +24,19 @@ func AcceptsNoArgs(args []string, cmd *cobra.Command) error {
35 35
 		cmd.Short,
36 36
 	)
37 37
 }
38
+
39
+// RequiresMinArgs returns an error if there is not at least min args
40
+func RequiresMinArgs(min int) cobra.PositionalArgs {
41
+	return func(cmd *cobra.Command, args []string) error {
42
+		if len(args) >= min {
43
+			return nil
44
+		}
45
+		return fmt.Errorf(
46
+			"\"%s\" requires at least %d argument(s).\n\nUsage:  %s\n\n%s",
47
+			cmd.CommandPath(),
48
+			min,
49
+			cmd.UseLine(),
50
+			cmd.Short,
51
+		)
52
+	}
53
+}
... ...
@@ -335,7 +335,7 @@ func testCommand(cmd string, newEnvs []string, scanForHome bool, home string) er
335 335
 			return fmt.Errorf("Should not have full usage on %q\n", args)
336 336
 		}
337 337
 		if strings.HasSuffix(stderr, "\n\n") {
338
-			return fmt.Errorf("Should not have a blank line on %q\n", args)
338
+			return fmt.Errorf("Should not have a blank line on %q\n%v", args, stderr)
339 339
 		}
340 340
 	}
341 341