Преглед на файлове

Merge pull request #23307 from WeiZhang555/cobra-restart

Migrate restart command to cobra
Sebastiaan van Stijn преди 9 години
родител
ревизия
88323c861a
променени са 5 файла, в които са добавени 53 реда и са изтрити 37 реда
  1. 0 1
      api/client/commands.go
  2. 52 0
      api/client/container/restart.go
  3. 0 35
      api/client/restart.go
  4. 1 0
      cli/cobraadaptor/adaptor.go
  5. 0 1
      cli/usage.go

+ 0 - 1
api/client/commands.go

@@ -17,7 +17,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
 		"ps":      cli.CmdPs,
 		"ps":      cli.CmdPs,
 		"pull":    cli.CmdPull,
 		"pull":    cli.CmdPull,
 		"push":    cli.CmdPush,
 		"push":    cli.CmdPush,
-		"restart": cli.CmdRestart,
 		"rm":      cli.CmdRm,
 		"rm":      cli.CmdRm,
 		"save":    cli.CmdSave,
 		"save":    cli.CmdSave,
 		"stats":   cli.CmdStats,
 		"stats":   cli.CmdStats,

+ 52 - 0
api/client/container/restart.go

@@ -0,0 +1,52 @@
+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 restartOptions struct {
+	nSeconds int
+
+	containers []string
+}
+
+// NewRestartCommand creats a new cobra.Command for `docker restart`
+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 {
+	var errs []string
+	for _, name := range opts.containers {
+		if err := dockerCli.Client().ContainerRestart(context.Background(), name, opts.nSeconds); err != nil {
+			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
+}

+ 0 - 35
api/client/restart.go

@@ -1,35 +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"
-)
-
-// CmdRestart restarts one or more containers.
-//
-// Usage: docker restart [OPTIONS] CONTAINER [CONTAINER...]
-func (cli *DockerCli) CmdRestart(args ...string) error {
-	cmd := Cli.Subcmd("restart", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["restart"].Description, true)
-	nSeconds := cmd.Int([]string{"t", "-time"}, 10, "Seconds to wait for stop before killing the container")
-	cmd.Require(flag.Min, 1)
-
-	cmd.ParseFlags(args, true)
-
-	var errs []string
-	for _, name := range cmd.Args() {
-		if err := cli.client.ContainerRestart(context.Background(), 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
-}

+ 1 - 0
cli/cobraadaptor/adaptor.go

@@ -43,6 +43,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
 		container.NewPauseCommand(dockerCli),
 		container.NewPauseCommand(dockerCli),
 		container.NewPortCommand(dockerCli),
 		container.NewPortCommand(dockerCli),
 		container.NewRenameCommand(dockerCli),
 		container.NewRenameCommand(dockerCli),
+		container.NewRestartCommand(dockerCli),
 		container.NewRunCommand(dockerCli),
 		container.NewRunCommand(dockerCli),
 		container.NewStartCommand(dockerCli),
 		container.NewStartCommand(dockerCli),
 		container.NewStopCommand(dockerCli),
 		container.NewStopCommand(dockerCli),

+ 0 - 1
cli/usage.go

@@ -22,7 +22,6 @@ var DockerCommandUsage = []Command{
 	{"ps", "List containers"},
 	{"ps", "List containers"},
 	{"pull", "Pull an image or a repository from a registry"},
 	{"pull", "Pull an image or a repository from a registry"},
 	{"push", "Push an image or a repository to a registry"},
 	{"push", "Push an image or a repository to a registry"},
-	{"restart", "Restart a container"},
 	{"rm", "Remove one or more containers"},
 	{"rm", "Remove one or more containers"},
 	{"save", "Save one or more images to a tar archive"},
 	{"save", "Save one or more images to a tar archive"},
 	{"stats", "Display a live stream of container(s) resource usage statistics"},
 	{"stats", "Display a live stream of container(s) resource usage statistics"},