mirror of
https://github.com/podman-container-tools/podman.git
synced 2026-10-11 08:27:36 +00:00
Table headings for `podman ps` and `podman pod ps` use a plain
`map[string]string`. A table template that calls `.Label` with an
argument fails while rendering the headings, before listing rows
are printed. For example: `podman ps --format 'table {{.Label "app"}}'`
Add a shared `PsHeader` type in `cmd/podman/common` with a `Label`
method that returns the requested label name as the column heading.
Use this type for both container and pod listings.
Add container unit tests and extend the existing container and pod
e2e tests to check label headings, label values, missing labels,
unlabeled objects, and `--noheading` output.
Fixes: #29831
Signed-off-by: Garv Changrani <154041471+Garvity@users.noreply.github.com>
29 lines
713 B
Go
29 lines
713 B
Go
package common
|
|
|
|
import (
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
// FormatLabels converts a map of labels to a sorted, comma-separated list
|
|
// of key=value pairs, matching Docker CLI output format.
|
|
func FormatLabels(labels map[string]string) string {
|
|
keys := make([]string, 0, len(labels))
|
|
for k := range labels {
|
|
keys = append(keys, k)
|
|
}
|
|
slices.Sort(keys)
|
|
list := make([]string, 0, len(keys))
|
|
for _, k := range keys {
|
|
list = append(list, k+"="+labels[k])
|
|
}
|
|
return strings.Join(list, ",")
|
|
}
|
|
|
|
// PsHeader provides column headings for container and pod listings.
|
|
type PsHeader map[string]string
|
|
|
|
// Label uses the requested label name as the column heading.
|
|
func (h PsHeader) Label(name string) string {
|
|
return name
|
|
}
|