diff options
author | baude <bbaude@redhat.com> | 2019-01-29 15:12:29 -0600 |
---|---|---|
committer | baude <bbaude@redhat.com> | 2019-01-30 08:32:28 -0600 |
commit | ca53122338168c867e75de0ce832d80ac7f27428 (patch) | |
tree | 6415911121b709e17bac8744ee13c3f639af872f /cmd/podman/ps.go | |
parent | ad5579e1d9905996612dd135467ee2ee5f62b7d3 (diff) | |
download | podman-ca53122338168c867e75de0ce832d80ac7f27428.tar.gz podman-ca53122338168c867e75de0ce832d80ac7f27428.tar.bz2 podman-ca53122338168c867e75de0ce832d80ac7f27428.zip |
Fix regression in ps with custom format
Using the table keyword in go templating had regressed and was
no longer working.
Fixes: 2221
Signed-off-by: baude <bbaude@redhat.com>
Diffstat (limited to 'cmd/podman/ps.go')
-rw-r--r-- | cmd/podman/ps.go | 41 |
1 files changed, 36 insertions, 5 deletions
diff --git a/cmd/podman/ps.go b/cmd/podman/ps.go index 0ad3f4c73..1708c671c 100644 --- a/cmd/podman/ps.go +++ b/cmd/podman/ps.go @@ -606,19 +606,50 @@ func portsToString(ports []ocicni.PortMapping) string { } func printFormat(format string, containers []shared.PsContainerOutput) error { - out := template.New("output") - out, err := out.Parse(format + "\n") + // return immediately if no containers are present + if len(containers) == 0 { + return nil + } + + // Use a tabwriter to align column format + w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) + + // Make a map of the field names for the headers + headerNames := make(map[string]string) + v := reflect.ValueOf(containers[0]) + t := v.Type() + for i := 0; i < t.NumField(); i++ { + headerNames[t.Field(i).Name] = t.Field(i).Name + } + + // Spit out the header if "table" is present in the format + if strings.HasPrefix(format, "table") { + hformat := strings.Replace(strings.TrimSpace(format[5:]), " ", "\t", -1) + format = hformat + headerTmpl, err := template.New("header").Parse(hformat) + if err != nil { + return err + } + if err := headerTmpl.Execute(w, headerNames); err != nil { + return err + } + fmt.Fprintln(w, "") + } + // Spit out the data rows now + dataTmpl, err := template.New("data").Parse(format) if err != nil { return err } + for _, container := range containers { - if err := out.Execute(os.Stdout, container); err != nil { + if err := dataTmpl.Execute(w, container); err != nil { return err } - + fmt.Fprintln(w, "") } - return nil + // Flush the writer + return w.Flush() } func dumpJSON(containers []shared.PsContainerOutput) error { |