瀏覽代碼

Use spf13/cobra for docker version

This fix is part of the effort to convert commands to spf13/cobra #23211.

Thif fix coverted command `docker version` to use spf13/cobra

NOTE: Most of the commands like `run`, `images` etc. goes to packages of
`container`, `image`, `network`, etc. Didn't find a good place for
`docker version` so just use the package `client` for now.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
Yong Tang 9 年之前
父節點
當前提交
bc82e51d77
共有 4 個文件被更改,包括 40 次插入25 次删除
  1. 0 1
      api/client/commands.go
  2. 38 23
      api/client/system/version.go
  3. 2 0
      cli/cobraadaptor/adaptor.go
  4. 0 1
      cli/usage.go

+ 0 - 1
api/client/commands.go

@@ -22,6 +22,5 @@ func (cli *DockerCli) Command(name string) func(...string) error {
 		"stats":   cli.CmdStats,
 		"tag":     cli.CmdTag,
 		"update":  cli.CmdUpdate,
-		"version": cli.CmdVersion,
 	}[name]
 }

+ 38 - 23
api/client/version.go → api/client/system/version.go

@@ -1,18 +1,18 @@
-package client
+package system
 
 import (
 	"runtime"
-	"text/template"
 	"time"
 
 	"golang.org/x/net/context"
 
-	Cli "github.com/docker/docker/cli"
+	"github.com/docker/docker/api/client"
+	"github.com/docker/docker/cli"
 	"github.com/docker/docker/dockerversion"
-	flag "github.com/docker/docker/pkg/mflag"
 	"github.com/docker/docker/utils"
 	"github.com/docker/docker/utils/templates"
 	"github.com/docker/engine-api/types"
+	"github.com/spf13/cobra"
 )
 
 var versionTemplate = `Client:
@@ -33,33 +33,48 @@ Server:
  OS/Arch:      {{.Server.Os}}/{{.Server.Arch}}{{if .Server.Experimental}}
  Experimental: {{.Server.Experimental}}{{end}}{{end}}`
 
-// CmdVersion shows Docker version information.
-//
-// Available version information is shown for: client Docker version, client API version, client Go version, client Git commit, client OS/Arch, server Docker version, server API version, server Go version, server Git commit, and server OS/Arch.
-//
-// Usage: docker version
-func (cli *DockerCli) CmdVersion(args ...string) (err error) {
-	cmd := Cli.Subcmd("version", nil, Cli.DockerCommands["version"].Description, true)
-	tmplStr := cmd.String([]string{"f", "#format", "-format"}, "", "Format the output using the given go template")
-	cmd.Require(flag.Exact, 0)
+type versionOptions struct {
+	format string
+}
+
+// NewVersionCommand creats a new cobra.Command for `docker version`
+func NewVersionCommand(dockerCli *client.DockerCli) *cobra.Command {
+	var opts versionOptions
+
+	cmd := &cobra.Command{
+		Use:   "version [OPTIONS]",
+		Short: "Show the Docker version information",
+		Args:  cli.ExactArgs(0),
+		RunE: func(cmd *cobra.Command, args []string) error {
+			return runVersion(dockerCli, &opts)
+		},
+	}
+
+	flags := cmd.Flags()
+
+	flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template")
+
+	return cmd
+}
 
-	cmd.ParseFlags(args, true)
+func runVersion(dockerCli *client.DockerCli, opts *versionOptions) error {
+	ctx := context.Background()
 
 	templateFormat := versionTemplate
-	if *tmplStr != "" {
-		templateFormat = *tmplStr
+	if opts.format != "" {
+		templateFormat = opts.format
 	}
 
-	var tmpl *template.Template
-	if tmpl, err = templates.Parse(templateFormat); err != nil {
-		return Cli.StatusError{StatusCode: 64,
+	tmpl, err := templates.Parse(templateFormat)
+	if err != nil {
+		return cli.StatusError{StatusCode: 64,
 			Status: "Template parsing error: " + err.Error()}
 	}
 
 	vd := types.VersionResponse{
 		Client: &types.Version{
 			Version:      dockerversion.Version,
-			APIVersion:   cli.client.ClientVersion(),
+			APIVersion:   dockerCli.Client().ClientVersion(),
 			GoVersion:    runtime.Version(),
 			GitCommit:    dockerversion.GitCommit,
 			BuildTime:    dockerversion.BuildTime,
@@ -69,7 +84,7 @@ func (cli *DockerCli) CmdVersion(args ...string) (err error) {
 		},
 	}
 
-	serverVersion, err := cli.client.ServerVersion(context.Background())
+	serverVersion, err := dockerCli.Client().ServerVersion(ctx)
 	if err == nil {
 		vd.Server = &serverVersion
 	}
@@ -87,9 +102,9 @@ func (cli *DockerCli) CmdVersion(args ...string) (err error) {
 		}
 	}
 
-	if err2 := tmpl.Execute(cli.out, vd); err2 != nil && err == nil {
+	if err2 := tmpl.Execute(dockerCli.Out(), vd); err2 != nil && err == nil {
 		err = err2
 	}
-	cli.out.Write([]byte{'\n'})
+	dockerCli.Out().Write([]byte{'\n'})
 	return err
 }

+ 2 - 0
cli/cobraadaptor/adaptor.go

@@ -5,6 +5,7 @@ import (
 	"github.com/docker/docker/api/client/container"
 	"github.com/docker/docker/api/client/image"
 	"github.com/docker/docker/api/client/network"
+	"github.com/docker/docker/api/client/system"
 	"github.com/docker/docker/api/client/volume"
 	"github.com/docker/docker/cli"
 	cliflags "github.com/docker/docker/cli/flags"
@@ -55,6 +56,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
 		image.NewSearchCommand(dockerCli),
 		image.NewImportCommand(dockerCli),
 		network.NewNetworkCommand(dockerCli),
+		system.NewVersionCommand(dockerCli),
 		volume.NewVolumeCommand(dockerCli),
 	)
 

+ 0 - 1
cli/usage.go

@@ -27,7 +27,6 @@ var DockerCommandUsage = []Command{
 	{"stats", "Display a live stream of container(s) resource usage statistics"},
 	{"tag", "Tag an image into a repository"},
 	{"update", "Update configuration of one or more containers"},
-	{"version", "Show the Docker version information"},
 }
 
 // DockerCommands stores all the docker command