forked from containers/podman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* --format "table {{.field..." will print fields out in a table with headings. Table keyword is removed, spaces between fields are converted to tabs * Update parse.MatchesJSONFormat()'s regex to be more inclusive * Add report.Headers(), obtain all the field names to be used as column headers, a map of field name to column headers may be provided to override the field names * Update several commands to use new functions Signed-off-by: Jhon Honce <[email protected]>
- Loading branch information
Showing
8 changed files
with
178 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package report | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestNormalizeFormat(t *testing.T) { | ||
cases := []struct { | ||
format string | ||
expected string | ||
}{ | ||
{"table {{.ID}}", "{{.ID}}\n"}, | ||
{"table {{.ID}} {{.C}}", "{{.ID}}\t{{.C}}\n"}, | ||
{"{{.ID}}", "{{.ID}}\n"}, | ||
{"{{.ID}}\n", "{{.ID}}\n"}, | ||
{"{{.ID}} {{.C}}", "{{.ID}} {{.C}}\n"}, | ||
{"\t{{.ID}}", "\t{{.ID}}\n"}, | ||
{`\t` + "{{.ID}}", "\t{{.ID}}\n"}, | ||
{"table {{.ID}}\t{{.C}}", "{{.ID}}\t{{.C}}\n"}, | ||
{"{{.ID}} table {{.C}}", "{{.ID}} table {{.C}}\n"}, | ||
} | ||
for _, tc := range cases { | ||
tc := tc | ||
|
||
label := strings.ReplaceAll(tc.format, " ", "<sp>") | ||
t.Run("NormalizeFormat/"+label, func(t *testing.T) { | ||
t.Parallel() | ||
actual := NormalizeFormat(tc.format) | ||
if actual != tc.expected { | ||
t.Errorf("Expected %q, actual %q", tc.expected, actual) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters