diff --git a/cli/command/system/testdata/docker-client-version.golden b/cli/command/system/testdata/docker-client-version.golden new file mode 100644 index 0000000000..8d2efad924 --- /dev/null +++ b/cli/command/system/testdata/docker-client-version.golden @@ -0,0 +1,9 @@ +Client: + Version: 18.99.5-ce + API version: 1.38 + Go version: go1.10.2 + Git commit: deadbeef + Built: Wed May 30 22:21:05 2018 + OS/Arch: linux/amd64 + Experimental: true + Orchestrator: swarm diff --git a/cli/command/system/version.go b/cli/command/system/version.go index 6d1ec243b8..04f060c4dc 100644 --- a/cli/command/system/version.go +++ b/cli/command/system/version.go @@ -14,6 +14,7 @@ import ( "github.com/docker/cli/kubernetes" "github.com/docker/cli/templates" "github.com/docker/docker/api/types" + "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cobra" kubernetesClient "k8s.io/client-go/kubernetes" @@ -121,19 +122,10 @@ func reformatDate(buildTime string) string { } func runVersion(dockerCli command.Cli, opts *versionOptions) error { - templateFormat := versionTemplate - tmpl := templates.New("version") - if opts.format != "" { - templateFormat = opts.format - } else { - tmpl = tmpl.Funcs(template.FuncMap{"getDetailsOrder": getDetailsOrder}) - } - var err error - tmpl, err = tmpl.Parse(templateFormat) + tmpl, err := newVersionTemplate(opts.format) if err != nil { - return cli.StatusError{StatusCode: 64, - Status: "Template parsing error: " + err.Error()} + return cli.StatusError{StatusCode: 64, Status: err.Error()} } vd := versionInfo{ @@ -200,15 +192,30 @@ func runVersion(dockerCli command.Cli, opts *versionOptions) error { }) } } - t := tabwriter.NewWriter(dockerCli.Out(), 15, 1, 1, ' ', 0) - if err2 := tmpl.Execute(t, vd); err2 != nil && err == nil { + if err2 := prettyPrintVersion(dockerCli, vd, tmpl); err2 != nil && err == nil { err = err2 } + return err +} + +func prettyPrintVersion(dockerCli command.Cli, vd versionInfo, tmpl *template.Template) error { + t := tabwriter.NewWriter(dockerCli.Out(), 15, 1, 1, ' ', 0) + err := tmpl.Execute(t, vd) t.Write([]byte("\n")) t.Flush() return err } +func newVersionTemplate(templateFormat string) (*template.Template, error) { + if templateFormat == "" { + templateFormat = versionTemplate + } + tmpl := templates.New("version").Funcs(template.FuncMap{"getDetailsOrder": getDetailsOrder}) + tmpl, err := tmpl.Parse(templateFormat) + + return tmpl, errors.Wrap(err, "Template parsing error") +} + func getDetailsOrder(v types.ComponentVersion) []string { out := make([]string, 0, len(v.Details)) for k := range v.Details { diff --git a/cli/command/system/version_test.go b/cli/command/system/version_test.go index fccfcfb2e3..81fc5b91af 100644 --- a/cli/command/system/version_test.go +++ b/cli/command/system/version_test.go @@ -8,6 +8,7 @@ import ( "github.com/gotestyourself/gotestyourself/assert" is "github.com/gotestyourself/gotestyourself/assert/cmp" + "github.com/gotestyourself/gotestyourself/golden" "github.com/docker/cli/internal/test" "github.com/docker/docker/api" @@ -44,6 +45,30 @@ func TestVersionWithOrchestrator(t *testing.T) { assert.Check(t, is.Contains(cleanTabs(cli.OutBuffer().String()), "Orchestrator: swarm")) } +func TestVersionAlign(t *testing.T) { + vi := versionInfo{ + Client: clientVersion{ + Version: "18.99.5-ce", + APIVersion: "1.38", + DefaultAPIVersion: "1.38", + GitCommit: "deadbeef", + GoVersion: "go1.10.2", + Os: "linux", + Arch: "amd64", + BuildTime: "Wed May 30 22:21:05 2018", + Experimental: true, + Orchestrator: "swarm", + }, + } + + cli := test.NewFakeCli(&fakeClient{}) + tmpl, err := newVersionTemplate("") + assert.NilError(t, err) + assert.NilError(t, prettyPrintVersion(cli, vi, tmpl)) + assert.Check(t, golden.String(cli.OutBuffer().String(), "docker-client-version.golden")) + assert.Check(t, is.Equal("", cli.ErrBuffer().String())) +} + func cleanTabs(line string) string { return strings.Join(strings.Fields(line), " ") }