Merge pull request #23285 from yongtang/23211-spf13-cobra-pause

Use spf13/cobra for docker pause
This commit is contained in:
Vincent Demeester 2016-06-07 10:01:42 +02:00
commit 6d4a7213e6
5 changed files with 52 additions and 38 deletions

View File

@ -16,7 +16,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
"load": cli.CmdLoad,
"login": cli.CmdLogin,
"logout": cli.CmdLogout,
"pause": cli.CmdPause,
"ps": cli.CmdPs,
"pull": cli.CmdPull,
"push": cli.CmdPush,

View File

@ -0,0 +1,51 @@
package container
import (
"fmt"
"strings"
"golang.org/x/net/context"
"github.com/docker/docker/api/client"
"github.com/docker/docker/cli"
"github.com/spf13/cobra"
)
type pauseOptions struct {
containers []string
}
// NewPauseCommand creats a new cobra.Command for `docker pause`
func NewPauseCommand(dockerCli *client.DockerCli) *cobra.Command {
var opts pauseOptions
cmd := &cobra.Command{
Use: "pause CONTAINER [CONTAINER...]",
Short: "Pause all processes within one or more containers",
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.containers = args
return runPause(dockerCli, &opts)
},
}
cmd.SetFlagErrorFunc(flagErrorFunc)
return cmd
}
func runPause(dockerCli *client.DockerCli, opts *pauseOptions) error {
ctx := context.Background()
var errs []string
for _, container := range opts.containers {
if err := dockerCli.Client().ContainerPause(ctx, container); err != nil {
errs = append(errs, err.Error())
} else {
fmt.Fprintf(dockerCli.Out(), "%s\n", container)
}
}
if len(errs) > 0 {
return fmt.Errorf("%s", strings.Join(errs, "\n"))
}
return nil
}

View File

@ -1,36 +0,0 @@
package client
import (
"fmt"
"strings"
"golang.org/x/net/context"
Cli "github.com/docker/docker/cli"
flag "github.com/docker/docker/pkg/mflag"
)
// CmdPause pauses all processes within one or more containers.
//
// Usage: docker pause CONTAINER [CONTAINER...]
func (cli *DockerCli) CmdPause(args ...string) error {
cmd := Cli.Subcmd("pause", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["pause"].Description, true)
cmd.Require(flag.Min, 1)
cmd.ParseFlags(args, true)
ctx := context.Background()
var errs []string
for _, name := range cmd.Args() {
if err := cli.client.ContainerPause(ctx, name); err != nil {
errs = append(errs, err.Error())
} else {
fmt.Fprintf(cli.out, "%s\n", name)
}
}
if len(errs) > 0 {
return fmt.Errorf("%s", strings.Join(errs, "\n"))
}
return nil
}

View File

@ -39,6 +39,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
container.NewDiffCommand(dockerCli),
container.NewExportCommand(dockerCli),
container.NewLogsCommand(dockerCli),
container.NewPauseCommand(dockerCli),
container.NewPortCommand(dockerCli),
container.NewRenameCommand(dockerCli),
container.NewRunCommand(dockerCli),

View File

@ -21,7 +21,6 @@ var DockerCommandUsage = []Command{
{"load", "Load an image from a tar archive or STDIN"},
{"login", "Log in to a Docker registry"},
{"logout", "Log out from a Docker registry"},
{"pause", "Pause all processes within a container"},
{"ps", "List containers"},
{"pull", "Pull an image or a repository from a registry"},
{"push", "Push an image or a repository to a registry"},