From bc82e51d7755e54dc6d0dcb93e6250bbff0bbd59 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Mon, 6 Jun 2016 19:15:37 -0700 Subject: [PATCH] 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 --- api/client/commands.go | 1 - api/client/{ => system}/version.go | 63 ++++++++++++++++++------------ cli/cobraadaptor/adaptor.go | 2 + cli/usage.go | 1 - 4 files changed, 41 insertions(+), 26 deletions(-) rename api/client/{ => system}/version.go (59%) diff --git a/api/client/commands.go b/api/client/commands.go index fcd5a311bb..23acc6a5e6 100644 --- a/api/client/commands.go +++ b/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] } diff --git a/api/client/version.go b/api/client/system/version.go similarity index 59% rename from api/client/version.go rename to api/client/system/version.go index ebec4def4a..2f3ae2d0b1 100644 --- a/api/client/version.go +++ b/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 +} - cmd.ParseFlags(args, true) +// NewVersionCommand creats a new cobra.Command for `docker version` +func NewVersionCommand(dockerCli *client.DockerCli) *cobra.Command { + var opts versionOptions - templateFormat := versionTemplate - if *tmplStr != "" { - templateFormat = *tmplStr + 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) + }, } - var tmpl *template.Template - if tmpl, err = templates.Parse(templateFormat); err != nil { - return Cli.StatusError{StatusCode: 64, + flags := cmd.Flags() + + flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template") + + return cmd +} + +func runVersion(dockerCli *client.DockerCli, opts *versionOptions) error { + ctx := context.Background() + + templateFormat := versionTemplate + if opts.format != "" { + templateFormat = opts.format + } + + 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 } diff --git a/cli/cobraadaptor/adaptor.go b/cli/cobraadaptor/adaptor.go index 0d536f0608..555f265ac0 100644 --- a/cli/cobraadaptor/adaptor.go +++ b/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), ) diff --git a/cli/usage.go b/cli/usage.go index baf4948931..1ad0a891e8 100644 --- a/cli/usage.go +++ b/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