mirror of https://github.com/docker/docs.git
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>
This commit is contained in:
parent
a01ae049f9
commit
bc82e51d77
|
|
@ -22,6 +22,5 @@ func (cli *DockerCli) Command(name string) func(...string) error {
|
||||||
"stats": cli.CmdStats,
|
"stats": cli.CmdStats,
|
||||||
"tag": cli.CmdTag,
|
"tag": cli.CmdTag,
|
||||||
"update": cli.CmdUpdate,
|
"update": cli.CmdUpdate,
|
||||||
"version": cli.CmdVersion,
|
|
||||||
}[name]
|
}[name]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,18 @@
|
||||||
package client
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"text/template"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
"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"
|
"github.com/docker/docker/dockerversion"
|
||||||
flag "github.com/docker/docker/pkg/mflag"
|
|
||||||
"github.com/docker/docker/utils"
|
"github.com/docker/docker/utils"
|
||||||
"github.com/docker/docker/utils/templates"
|
"github.com/docker/docker/utils/templates"
|
||||||
"github.com/docker/engine-api/types"
|
"github.com/docker/engine-api/types"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var versionTemplate = `Client:
|
var versionTemplate = `Client:
|
||||||
|
|
@ -33,33 +33,48 @@ Server:
|
||||||
OS/Arch: {{.Server.Os}}/{{.Server.Arch}}{{if .Server.Experimental}}
|
OS/Arch: {{.Server.Os}}/{{.Server.Arch}}{{if .Server.Experimental}}
|
||||||
Experimental: {{.Server.Experimental}}{{end}}{{end}}`
|
Experimental: {{.Server.Experimental}}{{end}}{{end}}`
|
||||||
|
|
||||||
// CmdVersion shows Docker version information.
|
type versionOptions struct {
|
||||||
//
|
format string
|
||||||
// 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)
|
|
||||||
|
|
||||||
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
|
cmd := &cobra.Command{
|
||||||
if *tmplStr != "" {
|
Use: "version [OPTIONS]",
|
||||||
templateFormat = *tmplStr
|
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
|
flags := cmd.Flags()
|
||||||
if tmpl, err = templates.Parse(templateFormat); err != nil {
|
|
||||||
return Cli.StatusError{StatusCode: 64,
|
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()}
|
Status: "Template parsing error: " + err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
vd := types.VersionResponse{
|
vd := types.VersionResponse{
|
||||||
Client: &types.Version{
|
Client: &types.Version{
|
||||||
Version: dockerversion.Version,
|
Version: dockerversion.Version,
|
||||||
APIVersion: cli.client.ClientVersion(),
|
APIVersion: dockerCli.Client().ClientVersion(),
|
||||||
GoVersion: runtime.Version(),
|
GoVersion: runtime.Version(),
|
||||||
GitCommit: dockerversion.GitCommit,
|
GitCommit: dockerversion.GitCommit,
|
||||||
BuildTime: dockerversion.BuildTime,
|
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 {
|
if err == nil {
|
||||||
vd.Server = &serverVersion
|
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
|
err = err2
|
||||||
}
|
}
|
||||||
cli.out.Write([]byte{'\n'})
|
dockerCli.Out().Write([]byte{'\n'})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"github.com/docker/docker/api/client/container"
|
"github.com/docker/docker/api/client/container"
|
||||||
"github.com/docker/docker/api/client/image"
|
"github.com/docker/docker/api/client/image"
|
||||||
"github.com/docker/docker/api/client/network"
|
"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/api/client/volume"
|
||||||
"github.com/docker/docker/cli"
|
"github.com/docker/docker/cli"
|
||||||
cliflags "github.com/docker/docker/cli/flags"
|
cliflags "github.com/docker/docker/cli/flags"
|
||||||
|
|
@ -55,6 +56,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
|
||||||
image.NewSearchCommand(dockerCli),
|
image.NewSearchCommand(dockerCli),
|
||||||
image.NewImportCommand(dockerCli),
|
image.NewImportCommand(dockerCli),
|
||||||
network.NewNetworkCommand(dockerCli),
|
network.NewNetworkCommand(dockerCli),
|
||||||
|
system.NewVersionCommand(dockerCli),
|
||||||
volume.NewVolumeCommand(dockerCli),
|
volume.NewVolumeCommand(dockerCli),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,6 @@ var DockerCommandUsage = []Command{
|
||||||
{"stats", "Display a live stream of container(s) resource usage statistics"},
|
{"stats", "Display a live stream of container(s) resource usage statistics"},
|
||||||
{"tag", "Tag an image into a repository"},
|
{"tag", "Tag an image into a repository"},
|
||||||
{"update", "Update configuration of one or more containers"},
|
{"update", "Update configuration of one or more containers"},
|
||||||
{"version", "Show the Docker version information"},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DockerCommands stores all the docker command
|
// DockerCommands stores all the docker command
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue