mirror of https://github.com/docker/docs.git
Use spf13/cobra for docker stop
This fix is part of the effort to convert commands to spf13/cobra #23211. Thif fix coverted command `docker stop` to use spf13/cobra Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
parent
0f13b69fe2
commit
63d66d2796
|
|
@ -40,7 +40,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
|
||||||
"save": cli.CmdSave,
|
"save": cli.CmdSave,
|
||||||
"start": cli.CmdStart,
|
"start": cli.CmdStart,
|
||||||
"stats": cli.CmdStats,
|
"stats": cli.CmdStats,
|
||||||
"stop": cli.CmdStop,
|
|
||||||
"tag": cli.CmdTag,
|
"tag": cli.CmdTag,
|
||||||
"top": cli.CmdTop,
|
"top": cli.CmdTop,
|
||||||
"unpause": cli.CmdUnpause,
|
"unpause": cli.CmdUnpause,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
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 stopOptions struct {
|
||||||
|
time int
|
||||||
|
|
||||||
|
containers []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewStopCommand creats a new cobra.Command for `docker stop`
|
||||||
|
func NewStopCommand(dockerCli *client.DockerCli) *cobra.Command {
|
||||||
|
var opts stopOptions
|
||||||
|
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "stop [OPTIONS] CONTAINER [CONTAINER...]",
|
||||||
|
Short: "Stop one or more running containers",
|
||||||
|
Args: cli.RequiresMinArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
opts.containers = args
|
||||||
|
return runStop(dockerCli, &opts)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cmd.SetFlagErrorFunc(flagErrorFunc)
|
||||||
|
|
||||||
|
flags := cmd.Flags()
|
||||||
|
flags.IntVarP(&opts.time, "time", "t", 10, "Seconds to wait for stop before killing it")
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func runStop(dockerCli *client.DockerCli, opts *stopOptions) error {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
var errs []string
|
||||||
|
for _, container := range opts.containers {
|
||||||
|
if err := dockerCli.Client().ContainerStop(ctx, container, opts.time); err != nil {
|
||||||
|
errs = append(errs, err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(dockerCli.Out(), "%s\n", container)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(errs) > 0 {
|
||||||
|
return fmt.Errorf("%s", strings.Join(errs, "\n"))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -1,39 +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"
|
|
||||||
)
|
|
||||||
|
|
||||||
// CmdStop stops one or more containers.
|
|
||||||
//
|
|
||||||
// A running container is stopped by first sending SIGTERM and then SIGKILL if the container fails to stop within a grace period (the default is 10 seconds).
|
|
||||||
//
|
|
||||||
// Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
|
|
||||||
func (cli *DockerCli) CmdStop(args ...string) error {
|
|
||||||
cmd := Cli.Subcmd("stop", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["stop"].Description+".\nSending SIGTERM and then SIGKILL after a grace period", true)
|
|
||||||
nSeconds := cmd.Int([]string{"t", "-time"}, 10, "Seconds to wait for stop before killing it")
|
|
||||||
cmd.Require(flag.Min, 1)
|
|
||||||
|
|
||||||
cmd.ParseFlags(args, true)
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
var errs []string
|
|
||||||
for _, name := range cmd.Args() {
|
|
||||||
if err := cli.client.ContainerStop(ctx, 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
|
|
||||||
}
|
|
||||||
|
|
@ -35,6 +35,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
|
||||||
rootCmd.AddCommand(
|
rootCmd.AddCommand(
|
||||||
container.NewCreateCommand(dockerCli),
|
container.NewCreateCommand(dockerCli),
|
||||||
container.NewRunCommand(dockerCli),
|
container.NewRunCommand(dockerCli),
|
||||||
|
container.NewStopCommand(dockerCli),
|
||||||
image.NewSearchCommand(dockerCli),
|
image.NewSearchCommand(dockerCli),
|
||||||
volume.NewVolumeCommand(dockerCli),
|
volume.NewVolumeCommand(dockerCli),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,6 @@ var DockerCommandUsage = []Command{
|
||||||
{"save", "Save one or more images to a tar archive"},
|
{"save", "Save one or more images to a tar archive"},
|
||||||
{"start", "Start one or more stopped containers"},
|
{"start", "Start one or more stopped containers"},
|
||||||
{"stats", "Display a live stream of container(s) resource usage statistics"},
|
{"stats", "Display a live stream of container(s) resource usage statistics"},
|
||||||
{"stop", "Stop a running container"},
|
|
||||||
{"tag", "Tag an image into a repository"},
|
{"tag", "Tag an image into a repository"},
|
||||||
{"top", "Display the running processes of a container"},
|
{"top", "Display the running processes of a container"},
|
||||||
{"unpause", "Unpause all processes within a container"},
|
{"unpause", "Unpause all processes within a container"},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue