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 ( import (
"fmt" "fmt"
"runtime" "runtime"
"sort"
"text/template"
"time" "time"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
@ -36,10 +38,11 @@ Server:{{if ne .Platform.Name ""}} {{.Platform.Name}}{{end}}
Built: {{index .Details "BuildTime"}} Built: {{index .Details "BuildTime"}}
OS/Arch: {{index .Details "Os"}}/{{index .Details "Arch"}} OS/Arch: {{index .Details "Os"}}/{{index .Details "Arch"}}
Experimental: {{index .Details "Experimental"}} Experimental: {{index .Details "Experimental"}}
{{- else -}} {{- else }}
Version: {{$component.Version}} Version: {{$component.Version}}
{{- range $k, $v := $component.Details}} {{- $detailsOrder := getDetailsOrder $component}}
{{$k}}: {{$v}} {{- range $key := $detailsOrder}}
{{$key}}: {{index $component.Details $key}}
{{- end}} {{- end}}
{{- end}} {{- end}}
{{- end}} {{- end}}
@ -106,11 +109,15 @@ func runVersion(dockerCli *command.DockerCli, opts *versionOptions) error {
ctx := context.Background() ctx := context.Background()
templateFormat := versionTemplate templateFormat := versionTemplate
tmpl := templates.New("version")
if opts.format != "" { if opts.format != "" {
templateFormat = 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 { if err != nil {
return cli.StatusError{StatusCode: 64, return cli.StatusError{StatusCode: 64,
Status: "Template parsing error: " + err.Error()} Status: "Template parsing error: " + err.Error()}
@ -172,3 +179,12 @@ func runVersion(dockerCli *command.DockerCli, opts *versionOptions) error {
dockerCli.Out().Write([]byte{'\n'}) dockerCli.Out().Write([]byte{'\n'})
return err 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) 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 // NewParse creates a new tagged template with the basic functions
// and parses the given format. // and parses the given format.
func NewParse(tag, format string) (*template.Template, error) { 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 // padWithSpace adds whitespace to the input if the input is non-empty