2016-06-06 10:15:46 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2016-06-08 02:05:18 -04:00
|
|
|
"time"
|
2016-06-06 10:15:46 -04:00
|
|
|
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2016-06-21 03:28:30 -04:00
|
|
|
// NewRestartCommand creates a new cobra.Command for `docker restart`
|
2016-06-06 10:15:46 -04:00
|
|
|
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 {
|
2016-06-09 11:58:10 -04:00
|
|
|
ctx := context.Background()
|
2016-06-06 10:15:46 -04:00
|
|
|
var errs []string
|
|
|
|
for _, name := range opts.containers {
|
2016-06-15 17:30:54 -04:00
|
|
|
timeout := time.Duration(opts.nSeconds) * time.Second
|
|
|
|
if err := dockerCli.Client().ContainerRestart(ctx, name, &timeout); err != nil {
|
2016-06-06 10:15:46 -04:00
|
|
|
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
|
|
|
|
}
|