Sort component details in template

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff 2017-12-06 15:35:43 -05:00 committed by Tibor Vass
parent 5f4c5f8bb6
commit 7138d6e301
2 changed files with 27 additions and 5 deletions

View File

@ -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
}

View File

@ -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