1- package client
1+ package system
22
33import (
44 "runtime"
5- "text/template"
65 "time"
76
87 "golang.org/x/net/context"
98
10- Cli "github.com/docker/docker/cli"
9+ "github.com/docker/docker/api/client"
10+ "github.com/docker/docker/cli"
1111 "github.com/docker/docker/dockerversion"
12- flag "github.com/docker/docker/pkg/mflag"
1312 "github.com/docker/docker/utils"
1413 "github.com/docker/docker/utils/templates"
1514 "github.com/docker/engine-api/types"
15+ "github.com/spf13/cobra"
1616)
1717
1818var versionTemplate = `Client:
@@ -33,33 +33,48 @@ Server:
3333 OS/Arch: {{.Server.Os}}/{{.Server.Arch}}{{if .Server.Experimental}}
3434 Experimental: {{.Server.Experimental}}{{end}}{{end}}`
3535
36- // CmdVersion shows Docker version information.
37- //
38- // 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.
39- //
40- // Usage: docker version
41- func (cli * DockerCli ) CmdVersion (args ... string ) (err error ) {
42- cmd := Cli .Subcmd ("version" , nil , Cli .DockerCommands ["version" ].Description , true )
43- tmplStr := cmd .String ([]string {"f" , "#format" , "-format" }, "" , "Format the output using the given go template" )
44- cmd .Require (flag .Exact , 0 )
36+ type versionOptions struct {
37+ format string
38+ }
39+
40+ // NewVersionCommand creats a new cobra.Command for `docker version`
41+ func NewVersionCommand (dockerCli * client.DockerCli ) * cobra.Command {
42+ var opts versionOptions
43+
44+ cmd := & cobra.Command {
45+ Use : "version [OPTIONS]" ,
46+ Short : "Show the Docker version information" ,
47+ Args : cli .ExactArgs (0 ),
48+ RunE : func (cmd * cobra.Command , args []string ) error {
49+ return runVersion (dockerCli , & opts )
50+ },
51+ }
52+
53+ flags := cmd .Flags ()
54+
55+ flags .StringVarP (& opts .format , "format" , "f" , "" , "Format the output using the given go template" )
56+
57+ return cmd
58+ }
4559
46- cmd .ParseFlags (args , true )
60+ func runVersion (dockerCli * client.DockerCli , opts * versionOptions ) error {
61+ ctx := context .Background ()
4762
4863 templateFormat := versionTemplate
49- if * tmplStr != "" {
50- templateFormat = * tmplStr
64+ if opts . format != "" {
65+ templateFormat = opts . format
5166 }
5267
53- var tmpl * template. Template
54- if tmpl , err = templates . Parse ( templateFormat ); err != nil {
55- return Cli .StatusError {StatusCode : 64 ,
68+ tmpl , err := templates . Parse ( templateFormat )
69+ if err != nil {
70+ return cli .StatusError {StatusCode : 64 ,
5671 Status : "Template parsing error: " + err .Error ()}
5772 }
5873
5974 vd := types.VersionResponse {
6075 Client : & types.Version {
6176 Version : dockerversion .Version ,
62- APIVersion : cli . client .ClientVersion (),
77+ APIVersion : dockerCli . Client () .ClientVersion (),
6378 GoVersion : runtime .Version (),
6479 GitCommit : dockerversion .GitCommit ,
6580 BuildTime : dockerversion .BuildTime ,
@@ -69,7 +84,7 @@ func (cli *DockerCli) CmdVersion(args ...string) (err error) {
6984 },
7085 }
7186
72- serverVersion , err := cli . client .ServerVersion (context . Background () )
87+ serverVersion , err := dockerCli . Client () .ServerVersion (ctx )
7388 if err == nil {
7489 vd .Server = & serverVersion
7590 }
@@ -87,9 +102,9 @@ func (cli *DockerCli) CmdVersion(args ...string) (err error) {
87102 }
88103 }
89104
90- if err2 := tmpl .Execute (cli . out , vd ); err2 != nil && err == nil {
105+ if err2 := tmpl .Execute (dockerCli . Out () , vd ); err2 != nil && err == nil {
91106 err = err2
92107 }
93- cli . out .Write ([]byte {'\n' })
108+ dockerCli . Out () .Write ([]byte {'\n' })
94109 return err
95110}
0 commit comments