Merge pull request #23307 from WeiZhang555/cobra-restart

Migrate restart command to cobra
This commit is contained in:
Sebastiaan van Stijn 2016-06-07 15:28:35 +02:00
commit 88323c861a
5 changed files with 53 additions and 37 deletions

View file

@ -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,

View file

@ -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
}

View file

@ -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
}

View file

@ -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),

View file

@ -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"},