diff --git a/api/client/commands.go b/api/client/commands.go index a3e0f06d07..bda04edd99 100644 --- a/api/client/commands.go +++ b/api/client/commands.go @@ -12,7 +12,6 @@ func (cli *DockerCli) Command(name string) func(...string) error { "import": cli.CmdImport, "info": cli.CmdInfo, "inspect": cli.CmdInspect, - "kill": cli.CmdKill, "load": cli.CmdLoad, "login": cli.CmdLogin, "logout": cli.CmdLogout, diff --git a/api/client/container/kill.go b/api/client/container/kill.go new file mode 100644 index 0000000000..7dd5d277bc --- /dev/null +++ b/api/client/container/kill.go @@ -0,0 +1,53 @@ +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 killOptions struct { + signal string + + containers []string +} + +// NewKillCommand creats a new cobra.Command for `docker kill` +func NewKillCommand(dockerCli *client.DockerCli) *cobra.Command { + var opts killOptions + + cmd := &cobra.Command{ + Use: "kill [OPTIONS] CONTAINER [CONTAINER...]", + Short: "Kill one or more running container", + Args: cli.RequiresMinArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + opts.containers = args + return runKill(dockerCli, &opts) + }, + } + + flags := cmd.Flags() + flags.StringVarP(&opts.signal, "signal", "s", "KILL", "Signal to send to the container") + return cmd +} + +func runKill(dockerCli *client.DockerCli, opts *killOptions) error { + var errs []string + ctx := context.Background() + for _, name := range opts.containers { + if err := dockerCli.Client().ContainerKill(ctx, name, opts.signal); err != nil { + errs = append(errs, err.Error()) + } else { + fmt.Fprintf(dockerCli.Out(), "%s\n", name) + } + } + if len(errs) > 0 { + return fmt.Errorf("%s", strings.Join(errs, "\n")) + } + return nil +} diff --git a/api/client/kill.go b/api/client/kill.go deleted file mode 100644 index 9841ba4d02..0000000000 --- a/api/client/kill.go +++ /dev/null @@ -1,35 +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" -) - -// CmdKill kills one or more running container using SIGKILL or a specified signal. -// -// Usage: docker kill [OPTIONS] CONTAINER [CONTAINER...] -func (cli *DockerCli) CmdKill(args ...string) error { - cmd := Cli.Subcmd("kill", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["kill"].Description, true) - signal := cmd.String([]string{"s", "-signal"}, "KILL", "Signal to send to the container") - cmd.Require(flag.Min, 1) - - cmd.ParseFlags(args, true) - - var errs []string - for _, name := range cmd.Args() { - if err := cli.client.ContainerKill(context.Background(), name, *signal); 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 -} diff --git a/cli/cobraadaptor/adaptor.go b/cli/cobraadaptor/adaptor.go index f371ce4ed7..2cb64e0541 100644 --- a/cli/cobraadaptor/adaptor.go +++ b/cli/cobraadaptor/adaptor.go @@ -38,6 +38,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor { container.NewCreateCommand(dockerCli), container.NewDiffCommand(dockerCli), container.NewExportCommand(dockerCli), + container.NewKillCommand(dockerCli), container.NewLogsCommand(dockerCli), container.NewPauseCommand(dockerCli), container.NewPortCommand(dockerCli), diff --git a/cli/usage.go b/cli/usage.go index 787a87aaee..727a7fb77a 100644 --- a/cli/usage.go +++ b/cli/usage.go @@ -17,7 +17,6 @@ var DockerCommandUsage = []Command{ {"import", "Import the contents from a tarball to create a filesystem image"}, {"info", "Display system-wide information"}, {"inspect", "Return low-level information on a container or image"}, - {"kill", "Kill a running container"}, {"load", "Load an image from a tar archive or STDIN"}, {"login", "Log in to a Docker registry"}, {"logout", "Log out from a Docker registry"},