8 "github.com/docker/cli/cli"
9 "github.com/docker/cli/cli/command"
10 "github.com/pkg/errors"
11 "github.com/spf13/cobra"
12 "golang.org/x/net/context"
15 type restartOptions struct {
22 // NewRestartCommand creates a new cobra.Command for `docker restart`
23 func NewRestartCommand(dockerCli *command.DockerCli) *cobra.Command {
24 var opts restartOptions
26 cmd := &cobra.Command{
27 Use: "restart [OPTIONS] CONTAINER [CONTAINER...]",
28 Short: "Restart one or more containers",
29 Args: cli.RequiresMinArgs(1),
30 RunE: func(cmd *cobra.Command, args []string) error {
31 opts.containers = args
32 opts.nSecondsChanged = cmd.Flags().Changed("time")
33 return runRestart(dockerCli, &opts)
38 flags.IntVarP(&opts.nSeconds, "time", "t", 10, "Seconds to wait for stop before killing the container")
42 func runRestart(dockerCli *command.DockerCli, opts *restartOptions) error {
43 ctx := context.Background()
45 var timeout *time.Duration
46 if opts.nSecondsChanged {
47 timeoutValue := time.Duration(opts.nSeconds) * time.Second
48 timeout = &timeoutValue
51 for _, name := range opts.containers {
52 if err := dockerCli.Client().ContainerRestart(ctx, name, timeout); err != nil {
53 errs = append(errs, err.Error())
56 fmt.Fprintln(dockerCli.Out(), name)
59 return errors.New(strings.Join(errs, "\n"))