-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils.go
357 lines (305 loc) · 8 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package utils
import (
"archive/tar"
"bytes"
"compress/gzip"
crypto "crypto/rand"
"encoding/json"
"errors"
"fmt"
"io"
"math/big"
"math/rand"
"net/http"
"os"
"reflect"
"sort"
"strings"
"time"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/modern-go/reflect2"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"sigs.k8s.io/yaml"
ocmlog "github.com/open-component-model/ocm/pkg/logging"
)
// PrintPrettyYaml prints the given objects as yaml if enabled.
func PrintPrettyYaml(obj interface{}, enabled bool) {
if !enabled {
return
}
data, err := yaml.Marshal(obj)
if err != nil {
//nolint: forbidigo // Intentional Println to not mess up potential output parsers.
fmt.Println(err)
return
}
//nolint: forbidigo // Intentional Println.
fmt.Println(string(data))
}
// GetFileType returns the mimetype of a file.
func GetFileType(fs vfs.FileSystem, path string) (string, error) {
file, err := fs.Open(path)
if err != nil {
return "", err
}
defer file.Close()
// see http://golang.org/pkg/net/http/#DetectContentType for the 512 bytes
buf := make([]byte, 512)
_, err = file.Read(buf)
if err != nil {
return "", err
}
return http.DetectContentType(buf), nil
}
// CleanMarkdownUsageFunc removes Markdown tags from the long usage of the command.
// With this func it is possible to generate the Markdown docs but still have readable commandline help func.
// Note: currently only "<pre>" tags are removed.
func CleanMarkdownUsageFunc(cmd *cobra.Command) {
defaultHelpFunc := cmd.HelpFunc()
cmd.SetHelpFunc(func(cmd *cobra.Command, s []string) {
cmd.Long = strings.ReplaceAll(cmd.Long, "<pre>", "")
cmd.Long = strings.ReplaceAll(cmd.Long, "</pre>", "")
defaultHelpFunc(cmd, s)
})
}
// RawJSON converts an arbitrary value to json.RawMessage.
func RawJSON(value interface{}) (*json.RawMessage, error) {
jsonval, err := json.Marshal(value)
if err != nil {
return nil, err
}
return (*json.RawMessage)(&jsonval), nil
}
// Gzip applies gzip compression to an arbitrary byte slice.
func Gzip(data []byte, compressionLevel int) ([]byte, error) {
buf := bytes.NewBuffer([]byte{})
gzipWriter, err := gzip.NewWriterLevel(buf, compressionLevel)
if err != nil {
return nil, fmt.Errorf("unable to create gzip writer: %w", err)
}
defer gzipWriter.Close()
if _, err = gzipWriter.Write(data); err != nil {
return nil, fmt.Errorf("unable to write to stream: %w", err)
}
if err = gzipWriter.Close(); err != nil {
return nil, fmt.Errorf("unable to close writer: %w", err)
}
return buf.Bytes(), nil
}
var chars = []rune("abcdefghijklmnopqrstuvwxyz1234567890")
// RandomString creates a new random string with the given length.
func RandomString(n int) string {
b := make([]rune, n)
for i := range b {
var value int
if v, err := crypto.Int(crypto.Reader, big.NewInt(int64(len(chars)))); err == nil {
value = int(v.Int64())
} else {
// insecure fallback to provide a valid result
ocmlog.Logger().Error("failed to generate random number", "error", err.Error())
value = rand.Intn(len(chars)) //nolint: gosec // only used as fallback
}
b[i] = chars[value]
}
return string(b)
}
// SafeConvert converts a byte slice to string.
// If the byte slice is nil, an empty string is returned.
func SafeConvert(bytes []byte) string {
if bytes == nil {
return ""
}
return string(bytes)
}
const (
BYTE = 1.0 << (10 * iota)
KIBIBYTE
MEBIBYTE
GIBIBYTE
)
// BytesString converts bytes into a human-readable string.
// This function is inspired by https://www.reddit.com/r/golang/comments/8micn7/review_bytes_to_human_readable_format/
func BytesString(bytes uint64, accuracy int) string {
unit := ""
value := float32(bytes)
switch {
case bytes >= GIBIBYTE:
unit = "GiB"
value /= GIBIBYTE
case bytes >= MEBIBYTE:
unit = "MiB"
value /= MEBIBYTE
case bytes >= KIBIBYTE:
unit = "KiB"
value /= KIBIBYTE
case bytes >= BYTE:
unit = "bytes"
case bytes == 0:
return "0"
}
stringValue := strings.TrimSuffix(
fmt.Sprintf("%.2f", value), "."+strings.Repeat("0", accuracy),
)
return fmt.Sprintf("%s %s", stringValue, unit)
}
// WriteFileToTARArchive writes a new file with name=filename and content=contentReader to archiveWriter.
func WriteFileToTARArchive(filename string, contentReader io.Reader, archiveWriter *tar.Writer) error {
if filename == "" {
return errors.New("filename must not be empty")
}
if contentReader == nil {
return errors.New("contentReader must not be nil")
}
if archiveWriter == nil {
return errors.New("archiveWriter must not be nil")
}
tempfile, err := os.CreateTemp("", "")
if err != nil {
return fmt.Errorf("unable to create tempfile: %w", err)
}
defer func() {
tempfile.Close()
os.Remove(tempfile.Name())
}()
fsize, err := io.Copy(tempfile, contentReader)
if err != nil {
return fmt.Errorf("unable to copy content to tempfile: %w", err)
}
if _, err := tempfile.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("unable to seek to beginning of tempfile: %w", err)
}
header := tar.Header{
Name: filename,
Size: fsize,
Mode: 0o600,
ModTime: time.Now(),
}
if err := archiveWriter.WriteHeader(&header); err != nil {
return fmt.Errorf("unable to write tar header: %w", err)
}
if _, err := io.Copy(archiveWriter, tempfile); err != nil {
return fmt.Errorf("unable to write file to tar archive: %w", err)
}
return nil
}
func IndentLines(orig string, gap string, skipfirst ...bool) string {
return JoinIndentLines(strings.Split(strings.TrimPrefix(orig, "\n"), "\n"), gap, skipfirst...)
}
func JoinIndentLines(orig []string, gap string, skipfirst ...bool) string {
if len(orig) == 0 {
return ""
}
skip := false
for _, b := range skipfirst {
skip = skip || b
}
s := ""
if !skip {
s = gap
}
return s + strings.Join(orig, "\n"+gap)
}
func StringMapKeys[K ~string, E any](m map[K]E) []K {
if m == nil {
return nil
}
keys := maps.Keys(m)
slices.Sort(keys)
return keys
}
type Comparable[K any] interface {
Compare(o K) int
}
func Sort[K Comparable[K]](a []K) {
sort.Slice(a, func(i, j int) bool { return a[i].Compare(a[j]) < 0 })
}
func MapKeys[K comparable, E any](m map[K]E) []K {
if m == nil {
return nil
}
keys := []K{}
for k := range m {
keys = append(keys, k)
}
return keys
}
type ComparableMapKey[K any] interface {
Comparable[K]
comparable
}
func SortedMapKeys[K ComparableMapKey[K], E any](m map[K]E) []K {
if m == nil {
return nil
}
keys := []K{}
for k := range m {
keys = append(keys, k)
}
Sort(keys)
return keys
}
// Optional returns the first optional non-zero element given as variadic argument,
// if given, or the zero element as default.
func Optional[T any](list ...T) T {
var zero T
for _, e := range list {
if !reflect.DeepEqual(e, zero) {
return e
}
}
return zero
}
// OptionalDefaulted returns the first optional non-nil element given as variadic
// argument, or the given default element. For value types a given zero
// argument is excepted, also.
func OptionalDefaulted[T any](def T, list ...T) T {
for _, e := range list {
if !reflect2.IsNil(e) {
return e
}
}
return def
}
// OptionalDefaultedBool checks all args for true. If arg is given
// the given default is returned.
func OptionalDefaultedBool(def bool, list ...bool) bool {
if len(list) == 0 {
return def
}
for _, e := range list {
if e {
return e
}
}
return false
}
// GetOptionFlag returns the flag value used to set a bool option
// based on optionally specified explicit value(s).
// The default value is to enable the option (true).
func GetOptionFlag(list ...bool) bool {
return OptionalDefaultedBool(true, list...)
}
func IsNil(o interface{}) bool {
return reflect2.IsNil(o)
}
// Must expect a result to be provided without error.
func Must[T any](o T, err error) T {
if err != nil {
panic(fmt.Errorf("expected a %T, but got error %w", o, err))
}
return o
}
func IgnoreError(_ error) {
}
func BoolP[T ~bool](b T) *bool {
v := bool(b)
return &v
}
func AsBool(b *bool, def ...bool) bool {
if b == nil && len(def) > 0 {
return Optional(def...)
}
return b != nil && *b
}