diff --git a/api/client/commands.go b/api/client/commands.go index 3b9fb603f9..fcd5a311bb 100644 --- a/api/client/commands.go +++ b/api/client/commands.go @@ -17,7 +17,6 @@ func (cli *DockerCli) Command(name string) func(...string) error { "ps": cli.CmdPs, "pull": cli.CmdPull, "push": cli.CmdPush, - "restart": cli.CmdRestart, "rm": cli.CmdRm, "save": cli.CmdSave, "stats": cli.CmdStats, diff --git a/api/client/container/restart.go b/api/client/container/restart.go new file mode 100644 index 0000000000..7a4c5e273f --- /dev/null +++ b/api/client/container/restart.go @@ -0,0 +1,52 @@ +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 restartOptions struct { + nSeconds int + + containers []string +} + +// NewRestartCommand creats a new cobra.Command for `docker restart` +func NewRestartCommand(dockerCli *client.DockerCli) *cobra.Command { + var opts restartOptions + + cmd := &cobra.Command{ + Use: "restart [OPTIONS] CONTAINER [CONTAINER...]", + Short: "Restart a container", + Args: cli.RequiresMinArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + opts.containers = args + return runRestart(dockerCli, &opts) + }, + } + + flags := cmd.Flags() + flags.IntVarP(&opts.nSeconds, "time", "t", 10, "Seconds to wait for stop before killing the container") + return cmd +} + +func runRestart(dockerCli *client.DockerCli, opts *restartOptions) error { + var errs []string + for _, name := range opts.containers { + if err := dockerCli.Client().ContainerRestart(context.Background(), name, opts.nSeconds); 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/restart.go b/api/client/restart.go deleted file mode 100644 index c0a04bd10d..0000000000 --- a/api/client/restart.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" -) - -// CmdRestart restarts one or more containers. -// -// Usage: docker restart [OPTIONS] CONTAINER [CONTAINER...] -func (cli *DockerCli) CmdRestart(args ...string) error { - cmd := Cli.Subcmd("restart", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["restart"].Description, true) - nSeconds := cmd.Int([]string{"t", "-time"}, 10, "Seconds to wait for stop before killing the container") - cmd.Require(flag.Min, 1) - - cmd.ParseFlags(args, true) - - var errs []string - for _, name := range cmd.Args() { - if err := cli.client.ContainerRestart(context.Background(), name, *nSeconds); 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 d9ce1b4ef1..0d536f0608 100644 --- a/cli/cobraadaptor/adaptor.go +++ b/cli/cobraadaptor/adaptor.go @@ -43,6 +43,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor { container.NewPauseCommand(dockerCli), container.NewPortCommand(dockerCli), container.NewRenameCommand(dockerCli), + container.NewRestartCommand(dockerCli), container.NewRunCommand(dockerCli), container.NewStartCommand(dockerCli), container.NewStopCommand(dockerCli), diff --git a/cli/usage.go b/cli/usage.go index e5ebb5e25b..baf4948931 100644 --- a/cli/usage.go +++ b/cli/usage.go @@ -22,7 +22,6 @@ var DockerCommandUsage = []Command{ {"ps", "List containers"}, {"pull", "Pull an image or a repository from a registry"}, {"push", "Push an image or a repository to a registry"}, - {"restart", "Restart a container"}, {"rm", "Remove one or more containers"}, {"save", "Save one or more images to a tar archive"}, {"stats", "Display a live stream of container(s) resource usage statistics"},