summaryrefslogtreecommitdiff
path: root/cmd/podman/report/format.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2020-10-02 14:49:02 -0400
committerGitHub <noreply@github.com>2020-10-02 14:49:02 -0400
commitf1cdead33d8f9c29348f9a66ba4a6aa5ed0aa7b2 (patch)
tree1485fd4238fa9d3b8d64e1d13e54f0015300aeb7 /cmd/podman/report/format.go
parent1e162edc8a2dd3d8373128e671c578ffcad52272 (diff)
parentc0757374bf187edcf4ae8c4811e162e27794ebf8 (diff)
downloadpodman-f1cdead33d8f9c29348f9a66ba4a6aa5ed0aa7b2.tar.gz
podman-f1cdead33d8f9c29348f9a66ba4a6aa5ed0aa7b2.tar.bz2
podman-f1cdead33d8f9c29348f9a66ba4a6aa5ed0aa7b2.zip
Merge pull request #7199 from jwhonce/jira/run-898
Restore "table" --format from V1
Diffstat (limited to 'cmd/podman/report/format.go')
-rw-r--r--cmd/podman/report/format.go68
1 files changed, 68 insertions, 0 deletions
diff --git a/cmd/podman/report/format.go b/cmd/podman/report/format.go
new file mode 100644
index 000000000..32d92bec5
--- /dev/null
+++ b/cmd/podman/report/format.go
@@ -0,0 +1,68 @@
+package report
+
+import (
+ "reflect"
+ "strings"
+)
+
+// tableReplacer will remove 'table ' prefix and clean up tabs
+var tableReplacer = strings.NewReplacer(
+ "table ", "",
+ `\t`, "\t",
+ `\n`, "\n",
+ " ", "\t",
+)
+
+// escapedReplacer will clean up escaped characters from CLI
+var escapedReplacer = strings.NewReplacer(
+ `\t`, "\t",
+ `\n`, "\n",
+)
+
+// NormalizeFormat reads given go template format provided by CLI and munges it into what we need
+func NormalizeFormat(format string) string {
+ f := format
+ // two replacers used so we only remove the prefix keyword `table`
+ if strings.HasPrefix(f, "table ") {
+ f = tableReplacer.Replace(f)
+ } else {
+ f = escapedReplacer.Replace(format)
+ }
+
+ if !strings.HasSuffix(f, "\n") {
+ f += "\n"
+ }
+
+ return f
+}
+
+// Headers queries the interface for field names
+func Headers(object interface{}, overrides map[string]string) []map[string]string {
+ value := reflect.ValueOf(object)
+ if value.Kind() == reflect.Ptr {
+ value = value.Elem()
+ }
+
+ // Column header will be field name upper-cased.
+ headers := make(map[string]string, value.NumField())
+ for i := 0; i < value.Type().NumField(); i++ {
+ field := value.Type().Field(i)
+ // Recurse to find field names from promoted structs
+ if field.Type.Kind() == reflect.Struct && field.Anonymous {
+ h := Headers(reflect.New(field.Type).Interface(), nil)
+ for k, v := range h[0] {
+ headers[k] = v
+ }
+ continue
+ }
+ headers[field.Name] = strings.ToUpper(field.Name)
+ }
+
+ if len(overrides) > 0 {
+ // Override column header as provided
+ for k, v := range overrides {
+ headers[k] = strings.ToUpper(v)
+ }
+ }
+ return []map[string]string{headers}
+}