/
util.go
215 lines (196 loc) · 4.92 KB
/
util.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package cmd
import (
"bytes"
"fmt"
"io"
"strconv"
"strings"
"time"
"unicode"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
"sigs.k8s.io/structured-merge-diff/v4/value"
"sigs.k8s.io/yaml"
)
func getInfoOr(n *Node, defVal string) string {
for n != nil {
if len(n.Managers) > 0 {
var buf strings.Builder
for i, info := range n.Managers {
if i > 0 {
buf.WriteString("/\n")
}
buf.WriteString(info.String())
}
return buf.String()
}
n = n.Parent
}
return defVal
}
// fieldsToSet creates a set paths from an input trie of fields
func fieldsToSet(f metav1.FieldsV1) (s fieldpath.Set, err error) {
err = s.FromJSON(bytes.NewReader(f.Raw))
return s, err
}
func fieldListID(fl value.FieldList) string {
strs := make([]string, len(fl))
for i, k := range fl {
strs[i] = fmt.Sprintf("%v=%v", k.Name, value.ToString(k.Value))
}
return strings.Join(strs, ",")
}
func toYAMLString(s string) string {
data, _ := yaml.Marshal(s)
return string(data[:len(data)-1]) // remove the trailing newline
}
func onlyContainsNewline(s string) (onlyNewline bool) {
sawNewline := false
for _, ch := range s {
if ch == '\n' {
sawNewline = true
} else if unicode.IsControl(ch) {
return false
}
}
return sawNewline
}
func toYAMLStringValueln(prefix, s string, lvl int) string {
if onlyContainsNewline(s) {
var b strings.Builder
if strings.HasSuffix(s, "\n\n") {
b.WriteString("|+\n")
} else if strings.HasSuffix(s, "\n") {
b.WriteString("|\n")
} else {
b.WriteString("|-\n")
}
s = strings.TrimSuffix(s, "\n")
lines := strings.Split(s, "\n")
for _, line := range lines {
if len(line) > 0 {
b.WriteString(prefix)
writeIndent(&b, lvl)
b.WriteString(line)
}
b.WriteRune('\n')
}
return b.String()
}
return toYAMLString(s) + "\n"
}
func writeString(w io.Writer, v string) error {
_, err := w.Write([]byte(v))
return err
}
func writeBytes(w io.Writer, v ...byte) error {
_, err := w.Write(v)
return err
}
func formatBasicType(v interface{}) string {
switch actual := v.(type) {
case int:
return strconv.FormatInt(int64(actual), 10)
case int8:
return strconv.FormatInt(int64(actual), 10)
case int16:
return strconv.FormatInt(int64(actual), 10)
case int32:
return strconv.FormatInt(int64(actual), 10)
case int64:
return strconv.FormatInt(actual, 10)
case uint:
return strconv.FormatUint(uint64(actual), 10)
case uint8:
return strconv.FormatUint(uint64(actual), 10)
case uint16:
return strconv.FormatUint(uint64(actual), 10)
case uint32:
return strconv.FormatUint(uint64(actual), 10)
case uint64:
return strconv.FormatUint(actual, 10)
case bool:
return strconv.FormatBool(actual)
case float32:
return strconv.FormatFloat(float64(actual), 'f', -1, 64)
case float64:
return strconv.FormatFloat(actual, 'f', -1, 64)
default:
panic("unreachable")
}
}
func writeIndent(w io.Writer, lvl int) error {
length := lvl * 2
data := make([]byte, length)
for i := 0; i < length; i++ {
data[i] = ' '
}
_, err := w.Write(data)
return err
}
func appendSpace(s string, totalLen int) string {
return addSpace(s, totalLen, false)
}
func prependSpace(s string, totalLen int) string {
return addSpace(s, totalLen, true)
}
func addSpace(s string, totalLen int, front bool) string {
if len(s) >= totalLen {
return s
}
var b strings.Builder
b.Grow(totalLen)
n := totalLen - len(s)
if front {
for i := 0; i < n; i++ {
b.WriteByte(' ')
}
b.WriteString(s)
} else {
b.WriteString(s)
for i := 0; i < n; i++ {
b.WriteByte(' ')
}
}
return b.String()
}
func fieldListMatchObject(fl value.FieldList, o map[string]interface{}) bool {
for _, field := range fl {
i, exist := o[field.Name]
if !exist {
return false
}
gotVal := value.NewValueInterface(i)
if value.Compare(field.Value, gotVal) != 0 {
return false
}
}
return true
}
// humanDuration returns a human-readable approximation of a duration
// (eg. "About a minute", "4 hours ago", etc.).
// Copied from https://github.com/docker/go-units/blob/519db1ee28dcc9fd2474ae59fca29a810482bfb1/duration.go#L12
func humanDuration(d time.Duration) string {
if seconds := int(d.Seconds()); seconds < 1 {
return "Less than a second ago"
} else if seconds == 1 {
return "1 second ago"
} else if seconds < 60 {
return fmt.Sprintf("%d seconds ago", seconds)
} else if minutes := int(d.Minutes()); minutes == 1 {
return "About a minute ago"
} else if minutes < 60 {
return fmt.Sprintf("%d minutes ago", minutes)
} else if hours := int(d.Hours() + 0.5); hours == 1 {
return "About an hour ago"
} else if hours < 48 {
return fmt.Sprintf("%d hours ago", hours)
} else if hours < 24*7*2 {
return fmt.Sprintf("%d days ago", hours/24)
} else if hours < 24*30*2 {
return fmt.Sprintf("%d weeks ago", hours/24/7)
} else if hours < 24*365*2 {
return fmt.Sprintf("%d months ago", hours/24/30)
}
return fmt.Sprintf("%d years ago", int(d.Hours())/24/365)
}