forked from projecteru2/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
60 lines (51 loc) · 1.11 KB
/
utils.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
package describe
import (
"encoding/json"
"fmt"
"strings"
"github.com/ghodss/yaml"
"github.com/jedib0t/go-pretty/v6/table"
)
// Format indicates the output format
// can be yaml / yml / json or empty as default
// default will be table
var Format string
func isJSON() bool {
return strings.ToLower(Format) == "json"
}
func isYAML() bool {
y := strings.ToLower(Format)
return y == "yaml" || y == "yml"
}
// actually i need a `zip longest` function
// like in python itertools
func toTableRows(rows [][]string) []table.Row {
total := len(rows)
maxLength := 0
for _, row := range rows {
if len(row) > maxLength {
maxLength = len(row)
}
}
rs := []table.Row{}
for i := 0; i < maxLength; i++ {
lines := []interface{}{}
for j := 0; j < total; j++ {
if i < len(rows[j]) {
lines = append(lines, rows[j][i])
} else {
lines = append(lines, "")
}
}
rs = append(rs, table.Row(lines))
}
return rs
}
func describeAsJSON(o interface{}) {
j, _ := json.MarshalIndent(o, "", " ")
fmt.Println(string(j))
}
func describeAsYAML(o interface{}) {
y, _ := yaml.Marshal(o)
fmt.Println(string(y))
}