Update `docker stop` and `docker restart` to allow not specifying timeout and use the one specified at container creation time.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang 2016-06-06 20:29:05 -07:00
parent 6566662d1b
commit c424fb0e3d
2 changed files with 20 additions and 6 deletions

View File

@ -14,6 +14,7 @@ import (
type restartOptions struct { type restartOptions struct {
nSeconds int nSeconds int
nSecondsChanged bool
containers []string containers []string
} }
@ -28,6 +29,7 @@ func NewRestartCommand(dockerCli *command.DockerCli) *cobra.Command {
Args: cli.RequiresMinArgs(1), Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
opts.containers = args opts.containers = args
opts.nSecondsChanged = cmd.Flags().Changed("time")
return runRestart(dockerCli, &opts) return runRestart(dockerCli, &opts)
}, },
} }
@ -40,9 +42,14 @@ func NewRestartCommand(dockerCli *command.DockerCli) *cobra.Command {
func runRestart(dockerCli *command.DockerCli, opts *restartOptions) error { func runRestart(dockerCli *command.DockerCli, opts *restartOptions) error {
ctx := context.Background() ctx := context.Background()
var errs []string var errs []string
var timeout *time.Duration
if opts.nSecondsChanged {
timeoutValue := time.Duration(opts.nSeconds) * time.Second
timeout = &timeoutValue
}
for _, name := range opts.containers { for _, name := range opts.containers {
timeout := time.Duration(opts.nSeconds) * time.Second if err := dockerCli.Client().ContainerRestart(ctx, name, timeout); err != nil {
if err := dockerCli.Client().ContainerRestart(ctx, name, &timeout); err != nil {
errs = append(errs, err.Error()) errs = append(errs, err.Error())
} else { } else {
fmt.Fprintf(dockerCli.Out(), "%s\n", name) fmt.Fprintf(dockerCli.Out(), "%s\n", name)

View File

@ -14,6 +14,7 @@ import (
type stopOptions struct { type stopOptions struct {
time int time int
timeChanged bool
containers []string containers []string
} }
@ -28,6 +29,7 @@ func NewStopCommand(dockerCli *command.DockerCli) *cobra.Command {
Args: cli.RequiresMinArgs(1), Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
opts.containers = args opts.containers = args
opts.timeChanged = cmd.Flags().Changed("time")
return runStop(dockerCli, &opts) return runStop(dockerCli, &opts)
}, },
} }
@ -39,12 +41,17 @@ func NewStopCommand(dockerCli *command.DockerCli) *cobra.Command {
func runStop(dockerCli *command.DockerCli, opts *stopOptions) error { func runStop(dockerCli *command.DockerCli, opts *stopOptions) error {
ctx := context.Background() ctx := context.Background()
timeout := time.Duration(opts.time) * time.Second
var timeout *time.Duration
if opts.timeChanged {
timeoutValue := time.Duration(opts.time) * time.Second
timeout = &timeoutValue
}
var errs []string var errs []string
errChan := parallelOperation(ctx, opts.containers, func(ctx context.Context, id string) error { errChan := parallelOperation(ctx, opts.containers, func(ctx context.Context, id string) error {
return dockerCli.Client().ContainerStop(ctx, id, &timeout) return dockerCli.Client().ContainerStop(ctx, id, timeout)
}) })
for _, container := range opts.containers { for _, container := range opts.containers {
if err := <-errChan; err != nil { if err := <-errChan; err != nil {