/
format.go
84 lines (65 loc) · 1.77 KB
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package cmdlib
// FormatType represents type of format
type FormatType int
const (
// FormatTypeDefault - Default format. Uses tabular for single items, and short for collections.
FormatTypeDefault FormatType = iota
// FormatTypeTabular - tabular
FormatTypeTabular FormatType = iota
// FormatTypeShort - Tabular but sub items are not printed
FormatTypeShort FormatType = iota
// FormatTypeList - List of key value pairs (same items as table)
FormatTypeList FormatType = iota
// FormatTypeFullJSON - Full JSON
FormatTypeFullJSON
)
// Scan from string
func (t *FormatType) Scan(str string) {
switch str {
case "default":
*t = FormatTypeDefault
case "table":
*t = FormatTypeTabular
case "short":
*t = FormatTypeShort
case "list":
*t = FormatTypeList
case "json":
*t = FormatTypeFullJSON
}
}
// FormatOptions - options for formatting
type FormatOptions struct {
Type FormatType
// NoHeader - skip printing header for tabular, csv format
NoHeader bool
}
// CustomFormatter function
type CustomFormatter func(v interface{}, fo FormatOptions) Output
// Formatter represents settings for formatting a type.
type Formatter struct {
// List of columns
Columns []Column
// CustomFn - custom format fn. Used if specified.
CustomFn CustomFormatter
// SubItems of the resource
SubItems []SubItem
}
// Column represents column of a resource.
type Column struct {
// Name of the column
Name string
// JsonPath to get the column
Path string
// Getter - Custom getter function
Getter func(interface{}) interface{}
}
// SubItem represents sub collection
type SubItem struct {
// Name of the collection
Name string
// FieldName of the struct that has the sub collection
FieldName string
// Formatter overrides the default formatter for this item
Formatter *Formatter
}