This fix is part of the effort to convert commands to spf13/cobra #23211.
Thif fix coverted command `docker wait` to use spf13/cobra
Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
| 38 | 37 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,52 @@ |
| 0 |
+package container |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "fmt" |
|
| 4 |
+ "strings" |
|
| 5 |
+ |
|
| 6 |
+ "golang.org/x/net/context" |
|
| 7 |
+ |
|
| 8 |
+ "github.com/docker/docker/api/client" |
|
| 9 |
+ "github.com/docker/docker/cli" |
|
| 10 |
+ "github.com/spf13/cobra" |
|
| 11 |
+) |
|
| 12 |
+ |
|
| 13 |
+type waitOptions struct {
|
|
| 14 |
+ containers []string |
|
| 15 |
+} |
|
| 16 |
+ |
|
| 17 |
+// NewWaitCommand creats a new cobra.Command for `docker wait` |
|
| 18 |
+func NewWaitCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|
| 19 |
+ var opts waitOptions |
|
| 20 |
+ |
|
| 21 |
+ cmd := &cobra.Command{
|
|
| 22 |
+ Use: "wait CONTAINER [CONTAINER...]", |
|
| 23 |
+ Short: "Block until a container stops, then print its exit code", |
|
| 24 |
+ Args: cli.RequiresMinArgs(1), |
|
| 25 |
+ RunE: func(cmd *cobra.Command, args []string) error {
|
|
| 26 |
+ opts.containers = args |
|
| 27 |
+ return runWait(dockerCli, &opts) |
|
| 28 |
+ }, |
|
| 29 |
+ } |
|
| 30 |
+ cmd.SetFlagErrorFunc(flagErrorFunc) |
|
| 31 |
+ |
|
| 32 |
+ return cmd |
|
| 33 |
+} |
|
| 34 |
+ |
|
| 35 |
+func runWait(dockerCli *client.DockerCli, opts *waitOptions) error {
|
|
| 36 |
+ ctx := context.Background() |
|
| 37 |
+ |
|
| 38 |
+ var errs []string |
|
| 39 |
+ for _, container := range opts.containers {
|
|
| 40 |
+ status, err := dockerCli.Client().ContainerWait(ctx, container) |
|
| 41 |
+ if err != nil {
|
|
| 42 |
+ errs = append(errs, err.Error()) |
|
| 43 |
+ } else {
|
|
| 44 |
+ fmt.Fprintf(dockerCli.Out(), "%d\n", status) |
|
| 45 |
+ } |
|
| 46 |
+ } |
|
| 47 |
+ if len(errs) > 0 {
|
|
| 48 |
+ return fmt.Errorf("%s", strings.Join(errs, "\n"))
|
|
| 49 |
+ } |
|
| 50 |
+ return nil |
|
| 51 |
+} |
| 0 | 52 |
deleted file mode 100644 |
| ... | ... |
@@ -1,39 +0,0 @@ |
| 1 |
-package client |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "fmt" |
|
| 5 |
- "strings" |
|
| 6 |
- |
|
| 7 |
- "golang.org/x/net/context" |
|
| 8 |
- |
|
| 9 |
- Cli "github.com/docker/docker/cli" |
|
| 10 |
- flag "github.com/docker/docker/pkg/mflag" |
|
| 11 |
-) |
|
| 12 |
- |
|
| 13 |
-// CmdWait blocks until a container stops, then prints its exit code. |
|
| 14 |
-// |
|
| 15 |
-// If more than one container is specified, this will wait synchronously on each container. |
|
| 16 |
-// |
|
| 17 |
-// Usage: docker wait CONTAINER [CONTAINER...] |
|
| 18 |
-func (cli *DockerCli) CmdWait(args ...string) error {
|
|
| 19 |
- cmd := Cli.Subcmd("wait", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["wait"].Description, true)
|
|
| 20 |
- cmd.Require(flag.Min, 1) |
|
| 21 |
- |
|
| 22 |
- cmd.ParseFlags(args, true) |
|
| 23 |
- |
|
| 24 |
- ctx := context.Background() |
|
| 25 |
- |
|
| 26 |
- var errs []string |
|
| 27 |
- for _, name := range cmd.Args() {
|
|
| 28 |
- status, err := cli.client.ContainerWait(ctx, name) |
|
| 29 |
- if err != nil {
|
|
| 30 |
- errs = append(errs, err.Error()) |
|
| 31 |
- } else {
|
|
| 32 |
- fmt.Fprintf(cli.out, "%d\n", status) |
|
| 33 |
- } |
|
| 34 |
- } |
|
| 35 |
- if len(errs) > 0 {
|
|
| 36 |
- return fmt.Errorf("%s", strings.Join(errs, "\n"))
|
|
| 37 |
- } |
|
| 38 |
- return nil |
|
| 39 |
-} |
| ... | ... |
@@ -42,6 +42,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
|
| 42 | 42 |
container.NewStartCommand(dockerCli), |
| 43 | 43 |
container.NewStopCommand(dockerCli), |
| 44 | 44 |
container.NewUnpauseCommand(dockerCli), |
| 45 |
+ container.NewWaitCommand(dockerCli), |
|
| 45 | 46 |
image.NewRemoveCommand(dockerCli), |
| 46 | 47 |
image.NewSearchCommand(dockerCli), |
| 47 | 48 |
network.NewNetworkCommand(dockerCli), |
| ... | ... |
@@ -37,7 +37,6 @@ var DockerCommandUsage = []Command{
|
| 37 | 37 |
{"top", "Display the running processes of a container"},
|
| 38 | 38 |
{"update", "Update configuration of one or more containers"},
|
| 39 | 39 |
{"version", "Show the Docker version information"},
|
| 40 |
- {"wait", "Block until a container stops, then print its exit code"},
|
|
| 41 | 40 |
} |
| 42 | 41 |
|
| 43 | 42 |
// DockerCommands stores all the docker command |