mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
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>
This commit is contained in:
parent
3d80884f3d
commit
91731706c2
5 changed files with 52 additions and 38 deletions
|
@ -18,7 +18,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
|
||||||
"load": cli.CmdLoad,
|
"load": cli.CmdLoad,
|
||||||
"login": cli.CmdLogin,
|
"login": cli.CmdLogin,
|
||||||
"logout": cli.CmdLogout,
|
"logout": cli.CmdLogout,
|
||||||
"pause": cli.CmdPause,
|
|
||||||
"ps": cli.CmdPs,
|
"ps": cli.CmdPs,
|
||||||
"pull": cli.CmdPull,
|
"pull": cli.CmdPull,
|
||||||
"push": cli.CmdPush,
|
"push": cli.CmdPush,
|
||||||
|
|
51
api/client/container/pause.go
Normal file
51
api/client/container/pause.go
Normal 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
|
||||||
|
}
|
|
@ -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
|
|
||||||
}
|
|
|
@ -38,6 +38,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
|
||||||
container.NewDiffCommand(dockerCli),
|
container.NewDiffCommand(dockerCli),
|
||||||
container.NewExportCommand(dockerCli),
|
container.NewExportCommand(dockerCli),
|
||||||
container.NewLogsCommand(dockerCli),
|
container.NewLogsCommand(dockerCli),
|
||||||
|
container.NewPauseCommand(dockerCli),
|
||||||
container.NewPortCommand(dockerCli),
|
container.NewPortCommand(dockerCli),
|
||||||
container.NewRunCommand(dockerCli),
|
container.NewRunCommand(dockerCli),
|
||||||
container.NewStartCommand(dockerCli),
|
container.NewStartCommand(dockerCli),
|
||||||
|
|
|
@ -23,7 +23,6 @@ var DockerCommandUsage = []Command{
|
||||||
{"load", "Load an image from a tar archive or STDIN"},
|
{"load", "Load an image from a tar archive or STDIN"},
|
||||||
{"login", "Log in to a Docker registry"},
|
{"login", "Log in to a Docker registry"},
|
||||||
{"logout", "Log out from a Docker registry"},
|
{"logout", "Log out from a Docker registry"},
|
||||||
{"pause", "Pause all processes within a container"},
|
|
||||||
{"ps", "List containers"},
|
{"ps", "List containers"},
|
||||||
{"pull", "Pull an image or a repository from a registry"},
|
{"pull", "Pull an image or a repository from a registry"},
|
||||||
{"push", "Push an image or a repository to a registry"},
|
{"push", "Push an image or a repository to a registry"},
|
||||||
|
|
Loading…
Add table
Reference in a new issue