Fix docker ps table headers with custom format and "split" or "join"

Update the list of overrides for table headers so that columns using split or
join will produce the correct table header.

Before this patch:

    docker ps --format='table {{split .Names "/"}}'
    [NAMES]
    [unruffled_mclean]
    [eloquent_meitner]
    [sleepy_grothendieck]

With this patch applied:

    docker ps --format='table {{split .Names "/"}}'
    NAMES
    [unruffled_mclean]
    [eloquent_meitner]
    [sleepy_grothendieck]

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2019-12-24 17:32:44 +01:00
parent 69f216f6e4
commit aef6b04a7c
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 17 additions and 1 deletions

View File

@ -241,6 +241,10 @@ size: 0B
Context{Format: NewContainerFormat(`table {{truncate .ID 5}}\t{{json .Image}} {{.RunningFor}}/{{title .Status}}/{{pad .Ports 2 2}}.{{upper .Names}} {{lower .Status}}`, false, true)}, Context{Format: NewContainerFormat(`table {{truncate .ID 5}}\t{{json .Image}} {{.RunningFor}}/{{title .Status}}/{{pad .Ports 2 2}}.{{upper .Names}} {{lower .Status}}`, false, true)},
string(golden.Get(t, "container-context-write-special-headers.golden")), string(golden.Get(t, "container-context-write-special-headers.golden")),
}, },
{
Context{Format: NewContainerFormat(`table {{split .Image ":"}}`, false, false)},
"IMAGE\n[ubuntu]\n[ubuntu]\n",
},
} }
for _, testcase := range cases { for _, testcase := range cases {

View File

@ -30,11 +30,23 @@ var basicFunctions = template.FuncMap{
// HeaderFunctions are used to created headers of a table. // HeaderFunctions are used to created headers of a table.
// This is a replacement of basicFunctions for header generation // This is a replacement of basicFunctions for header generation
// because we want the header to remain intact. // because we want the header to remain intact.
// Some functions like `split` are irrelevant so not added. // Some functions like `pad` are not overridden (to preserve alignment
// with the columns).
var HeaderFunctions = template.FuncMap{ var HeaderFunctions = template.FuncMap{
"json": func(v string) string { "json": func(v string) string {
return v return v
}, },
"split": func(v string, _ string) string {
// we want the table header to show the name of the column, and not
// split the table header itself. Using a different signature
// here, and return a string instead of []string
return v
},
"join": func(v string, _ string) string {
// table headers are always a string, so use a different signature
// for the "join" function (string instead of []string)
return v
},
"title": func(v string) string { "title": func(v string) string {
return v return v
}, },