From 7138d6e3011deb4b7216130f64679612b49bdd75 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Wed, 6 Dec 2017 15:35:43 -0500 Subject: [PATCH] Sort component details in template Signed-off-by: Brian Goff --- cli/command/system/version.go | 24 ++++++++++++++++++++---- templates/templates.go | 8 +++++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/cli/command/system/version.go b/cli/command/system/version.go index 0cb5b6968f..d063045d5c 100644 --- a/cli/command/system/version.go +++ b/cli/command/system/version.go @@ -3,6 +3,8 @@ package system import ( "fmt" "runtime" + "sort" + "text/template" "time" "github.com/docker/cli/cli" @@ -36,10 +38,11 @@ Server:{{if ne .Platform.Name ""}} {{.Platform.Name}}{{end}} Built: {{index .Details "BuildTime"}} OS/Arch: {{index .Details "Os"}}/{{index .Details "Arch"}} Experimental: {{index .Details "Experimental"}} - {{- else -}} + {{- else }} Version: {{$component.Version}} - {{- range $k, $v := $component.Details}} - {{$k}}: {{$v}} + {{- $detailsOrder := getDetailsOrder $component}} + {{- range $key := $detailsOrder}} + {{$key}}: {{index $component.Details $key}} {{- end}} {{- end}} {{- end}} @@ -106,11 +109,15 @@ func runVersion(dockerCli *command.DockerCli, opts *versionOptions) error { ctx := context.Background() templateFormat := versionTemplate + tmpl := templates.New("version") if opts.format != "" { templateFormat = opts.format + } else { + tmpl = tmpl.Funcs(template.FuncMap{"getDetailsOrder": getDetailsOrder}) } - tmpl, err := templates.Parse(templateFormat) + var err error + tmpl, err = tmpl.Parse(templateFormat) if err != nil { return cli.StatusError{StatusCode: 64, Status: "Template parsing error: " + err.Error()} @@ -172,3 +179,12 @@ func runVersion(dockerCli *command.DockerCli, opts *versionOptions) error { dockerCli.Out().Write([]byte{'\n'}) return err } + +func getDetailsOrder(v types.ComponentVersion) []string { + out := make([]string, 0, len(v.Details)) + for k := range v.Details { + out = append(out, k) + } + sort.Strings(out) + return out +} diff --git a/templates/templates.go b/templates/templates.go index 80cab5ed34..6cc2ec3608 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -55,10 +55,16 @@ func Parse(format string) (*template.Template, error) { return NewParse("", format) } +// New creates a new empty template with the provided tag and built-in +// template functions. +func New(tag string) *template.Template { + return template.New(tag).Funcs(basicFunctions) +} + // NewParse creates a new tagged template with the basic functions // and parses the given format. func NewParse(tag, format string) (*template.Template, error) { - return template.New(tag).Funcs(basicFunctions).Parse(format) + return New(tag).Parse(format) } // padWithSpace adds whitespace to the input if the input is non-empty