github.com/DaoCloud/dao@v0.0.0-20161212064103-c3dbfd13ee36/api/client/system/version.go (about)

     1  package system
     2  
     3  import (
     4  	"runtime"
     5  	"time"
     6  
     7  	"golang.org/x/net/context"
     8  
     9  	"github.com/docker/docker/api/client"
    10  	"github.com/docker/docker/cli"
    11  	"github.com/docker/docker/dockerversion"
    12  	"github.com/docker/docker/utils"
    13  	"github.com/docker/docker/utils/templates"
    14  	"github.com/docker/engine-api/types"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  var versionTemplate = `Client:
    19   版本:      {{.Client.Version}}
    20   API版本:  {{.Client.APIVersion}}
    21   Go 版本:   {{.Client.GoVersion}}
    22   Git提交号:   {{.Client.GitCommit}}
    23   构建:        {{.Client.BuildTime}}
    24   操作系统/架构:      {{.Client.Os}}/{{.Client.Arch}}{{if .Client.Experimental}}
    25   试验版: {{.Client.Experimental}}{{end}}{{if .ServerOK}}
    26  Server:
    27   版本:      {{.Server.Version}}
    28   API 版本:  {{.Server.APIVersion}}
    29   Go 版本:   {{.Server.GoVersion}}
    30   Git提交号:   {{.Server.GitCommit}}
    31   构建:        {{.Server.BuildTime}}
    32   操作系统/架构:      {{.Server.Os}}/{{.Server.Arch}}{{if .Server.Experimental}}
    33   试验版: {{.Server.Experimental}}{{end}}{{end}}`
    34  
    35  type versionOptions struct {
    36  	format string
    37  }
    38  
    39  // NewVersionCommand creats a new cobra.Command for `docker version`
    40  func NewVersionCommand(dockerCli *client.DockerCli) *cobra.Command {
    41  	var opts versionOptions
    42  
    43  	cmd := &cobra.Command{
    44  		Use:   "version [OPTIONS]",
    45  		Short: "显示Docker的版本信息",
    46  		Args:  cli.NoArgs,
    47  		RunE: func(cmd *cobra.Command, args []string) error {
    48  			return runVersion(dockerCli, &opts)
    49  		},
    50  	}
    51  
    52  	flags := cmd.Flags()
    53  
    54  	flags.StringVarP(&opts.format, "format", "f", "", "基于指定的Go语言模板格式化命令输出内容")
    55  
    56  	return cmd
    57  }
    58  
    59  func runVersion(dockerCli *client.DockerCli, opts *versionOptions) error {
    60  	ctx := context.Background()
    61  
    62  	templateFormat := versionTemplate
    63  	if opts.format != "" {
    64  		templateFormat = opts.format
    65  	}
    66  
    67  	tmpl, err := templates.Parse(templateFormat)
    68  	if err != nil {
    69  		return cli.StatusError{StatusCode: 64,
    70  			Status: "Template parsing error: " + err.Error()}
    71  	}
    72  
    73  	vd := types.VersionResponse{
    74  		Client: &types.Version{
    75  			Version:      dockerversion.Version,
    76  			APIVersion:   dockerCli.Client().ClientVersion(),
    77  			GoVersion:    runtime.Version(),
    78  			GitCommit:    dockerversion.GitCommit,
    79  			BuildTime:    dockerversion.BuildTime,
    80  			Os:           runtime.GOOS,
    81  			Arch:         runtime.GOARCH,
    82  			Experimental: utils.ExperimentalBuild(),
    83  		},
    84  	}
    85  
    86  	serverVersion, err := dockerCli.Client().ServerVersion(ctx)
    87  	if err == nil {
    88  		vd.Server = &serverVersion
    89  	}
    90  
    91  	// first we need to make BuildTime more human friendly
    92  	t, errTime := time.Parse(time.RFC3339Nano, vd.Client.BuildTime)
    93  	if errTime == nil {
    94  		vd.Client.BuildTime = t.Format(time.ANSIC)
    95  	}
    96  
    97  	if vd.ServerOK() {
    98  		t, errTime = time.Parse(time.RFC3339Nano, vd.Server.BuildTime)
    99  		if errTime == nil {
   100  			vd.Server.BuildTime = t.Format(time.ANSIC)
   101  		}
   102  	}
   103  
   104  	if err2 := tmpl.Execute(dockerCli.Out(), vd); err2 != nil && err == nil {
   105  		err = err2
   106  	}
   107  	dockerCli.Out().Write([]byte{'\n'})
   108  	return err
   109  }