Browse code

Use spf13/cobra for docker pause

This fix is part of the effort to convert commands to spf13/cobra #23211.

Thif fix coverted command `docker pause` to use spf13/cobra

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2016/06/06 08:12:59
Showing 5 changed files
... ...
@@ -18,7 +18,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
18 18
 		"load":    cli.CmdLoad,
19 19
 		"login":   cli.CmdLogin,
20 20
 		"logout":  cli.CmdLogout,
21
-		"pause":   cli.CmdPause,
22 21
 		"ps":      cli.CmdPs,
23 22
 		"pull":    cli.CmdPull,
24 23
 		"push":    cli.CmdPush,
25 24
new file mode 100644
... ...
@@ -0,0 +1,51 @@
0
+package container
1
+
2
+import (
3
+	"fmt"
4
+	"strings"
5
+
6
+	"golang.org/x/net/context"
7
+
8
+	"github.com/docker/docker/api/client"
9
+	"github.com/docker/docker/cli"
10
+	"github.com/spf13/cobra"
11
+)
12
+
13
+type pauseOptions struct {
14
+	containers []string
15
+}
16
+
17
+// NewPauseCommand creats a new cobra.Command for `docker pause`
18
+func NewPauseCommand(dockerCli *client.DockerCli) *cobra.Command {
19
+	var opts pauseOptions
20
+
21
+	cmd := &cobra.Command{
22
+		Use:   "pause CONTAINER [CONTAINER...]",
23
+		Short: "Pause all processes within one or more containers",
24
+		Args:  cli.RequiresMinArgs(1),
25
+		RunE: func(cmd *cobra.Command, args []string) error {
26
+			opts.containers = args
27
+			return runPause(dockerCli, &opts)
28
+		},
29
+	}
30
+	cmd.SetFlagErrorFunc(flagErrorFunc)
31
+
32
+	return cmd
33
+}
34
+
35
+func runPause(dockerCli *client.DockerCli, opts *pauseOptions) error {
36
+	ctx := context.Background()
37
+
38
+	var errs []string
39
+	for _, container := range opts.containers {
40
+		if err := dockerCli.Client().ContainerPause(ctx, container); err != nil {
41
+			errs = append(errs, err.Error())
42
+		} else {
43
+			fmt.Fprintf(dockerCli.Out(), "%s\n", container)
44
+		}
45
+	}
46
+	if len(errs) > 0 {
47
+		return fmt.Errorf("%s", strings.Join(errs, "\n"))
48
+	}
49
+	return nil
50
+}
0 51
deleted file mode 100644
... ...
@@ -1,36 +0,0 @@
1
-package client
2
-
3
-import (
4
-	"fmt"
5
-	"strings"
6
-
7
-	"golang.org/x/net/context"
8
-
9
-	Cli "github.com/docker/docker/cli"
10
-	flag "github.com/docker/docker/pkg/mflag"
11
-)
12
-
13
-// CmdPause pauses all processes within one or more containers.
14
-//
15
-// Usage: docker pause CONTAINER [CONTAINER...]
16
-func (cli *DockerCli) CmdPause(args ...string) error {
17
-	cmd := Cli.Subcmd("pause", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["pause"].Description, true)
18
-	cmd.Require(flag.Min, 1)
19
-
20
-	cmd.ParseFlags(args, true)
21
-
22
-	ctx := context.Background()
23
-
24
-	var errs []string
25
-	for _, name := range cmd.Args() {
26
-		if err := cli.client.ContainerPause(ctx, name); err != nil {
27
-			errs = append(errs, err.Error())
28
-		} else {
29
-			fmt.Fprintf(cli.out, "%s\n", name)
30
-		}
31
-	}
32
-	if len(errs) > 0 {
33
-		return fmt.Errorf("%s", strings.Join(errs, "\n"))
34
-	}
35
-	return nil
36
-}
... ...
@@ -38,6 +38,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
38 38
 		container.NewDiffCommand(dockerCli),
39 39
 		container.NewExportCommand(dockerCli),
40 40
 		container.NewLogsCommand(dockerCli),
41
+		container.NewPauseCommand(dockerCli),
41 42
 		container.NewPortCommand(dockerCli),
42 43
 		container.NewRunCommand(dockerCli),
43 44
 		container.NewStartCommand(dockerCli),
... ...
@@ -23,7 +23,6 @@ var DockerCommandUsage = []Command{
23 23
 	{"load", "Load an image from a tar archive or STDIN"},
24 24
 	{"login", "Log in to a Docker registry"},
25 25
 	{"logout", "Log out from a Docker registry"},
26
-	{"pause", "Pause all processes within a container"},
27 26
 	{"ps", "List containers"},
28 27
 	{"pull", "Pull an image or a repository from a registry"},
29 28
 	{"push", "Push an image or a repository to a registry"},