/
utils.go
411 lines (368 loc) · 8.69 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package utils
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"time"
"github.com/fatih/structs"
"github.com/mitchellh/go-homedir"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
gcorecloud "github.com/G-Core/gcorelabscloud-go"
"github.com/G-Core/gcorelabscloud-go/gcore/task/v1/tasks"
)
var (
slPfx = fmt.Sprintf("sl:::%d:::", time.Now().UTC().UnixNano())
)
type EnumValue struct {
Enum []string
Default string
Destination *string
selected string
}
func (e *EnumValue) Set(value string) error {
for _, enum := range e.Enum {
if enum == value {
if e.Destination != nil {
e.Destination = &value
}
e.selected = value
return nil
}
}
return fmt.Errorf("allowed values are %s", strings.Join(e.Enum, ", "))
}
func (e EnumValue) String() string {
if e.selected == "" {
return e.Default
}
return e.selected
}
type EnumStringSliceValue struct {
Enum []string
Default string
selected []string
}
func (e *EnumStringSliceValue) Set(value string) error {
for _, enum := range e.Enum {
if enum == value {
e.selected = append(e.selected, value)
return nil
}
}
return fmt.Errorf("allowed values are %s", strings.Join(e.Enum, ", "))
}
func (e *EnumStringSliceValue) String() string {
if len(e.selected) == 0 {
return fmt.Sprintf("%s", []string{e.Default})
}
return fmt.Sprintf("%s", e.selected)
}
// Serialize allows EnumStringSliceValue to fulfill Serializer
func (e EnumStringSliceValue) Serialize() string {
jsonBytes, _ := json.Marshal(e.selected)
return fmt.Sprintf("%s%s", slPfx, string(jsonBytes))
}
// Value returns the slice of strings set by this flag
func (e EnumStringSliceValue) Value() []string {
return e.selected
}
// Get returns the slice of strings set by this flag
func (e EnumStringSliceValue) Get() interface{} {
return e
}
func GetEnumStringSliceValue(c *cli.Context, name string) []string {
return c.Value(name).(EnumStringSliceValue).Value()
}
func tableHeaderFromStruct(m interface{}) []string {
return structs.Names(m)
}
func tableRowFromStruct(m interface{}) []string {
var res []string
values := structs.Values(m)
for _, v := range values {
value, _ := json.Marshal(v)
res = append(res, string(value))
}
return res
}
func renderTable(input interface{}) {
if input == nil {
return
}
results := interfaceToSlice(input)
if len(results) == 0 {
return
}
res := results[0]
if reflect.TypeOf(res).Kind() != reflect.Struct {
for i := 0; i < len(results); i++ {
results[i] = struct {
Value string
}{Value: reflect.ValueOf(results[i]).String()}
}
res = results[0]
}
headers := tableHeaderFromStruct(res)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(headers)
for _, res := range results {
table.Append(tableRowFromStruct(res))
}
table.Render()
}
func interfaceToSlice(input interface{}) []interface{} {
var records []interface{}
if input == nil {
return records
}
object := reflect.ValueOf(input)
if reflect.TypeOf(input).Kind() != reflect.Slice {
records = append(records, input)
return records
}
for i := 0; i < object.Len(); i++ {
records = append(records, object.Index(i).Interface())
}
return records
}
func renderJSON(input interface{}) error {
if input == nil || (reflect.TypeOf(input).Kind() == reflect.Slice && reflect.ValueOf(input).Len() == 0) {
return nil
}
res, err := json.MarshalIndent(input, "", " ")
if err != nil {
return err
}
fmt.Println(string(res))
return nil
}
func renderYAML(input interface{}) {
if input == nil || (reflect.TypeOf(input).Kind() == reflect.Slice && reflect.ValueOf(input).Len() == 0) {
return
}
res, _ := yaml.Marshal(input)
fmt.Println(string(res))
}
func ShowResults(input interface{}, format string) {
switch format {
case "json":
err := renderJSON(input)
if err != nil {
fmt.Println(err)
}
case "table":
renderTable(input)
case "yaml":
renderYAML(input)
}
}
func StringToPointer(s string) *string {
if s == "" {
return nil
}
return &s
}
func IntToPointer(i int) *int {
if i == 0 {
return nil
}
return &i
}
func BoolToPointer(b bool) *bool {
if !b {
return nil
}
return &b
}
func StringFromIndex(content []string, idx int, defaultValue string) string {
if idx < len(content) {
return content[idx]
}
return defaultValue
}
func IntFromIndex(content []int, idx int, defaultValue int) int {
if idx < len(content) {
return content[idx]
}
return defaultValue
}
func StringSliceToMapInterface(slice []string) (map[string]interface{}, error) {
m := make(map[string]interface{})
for _, s := range slice {
parts := strings.SplitN(s, "=", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("wrong label format: %s", s)
}
m[parts[0]] = parts[1]
}
return m, nil
}
func WaitTaskAndShowResult(
c *cli.Context,
client *gcorecloud.ServiceClient,
results *tasks.TaskResults,
stopOnTaskError bool,
infoRetriever tasks.RetrieveTaskResult,
) error {
if c.Bool("wait") {
if len(results.Tasks) == 0 {
return cli.NewExitError(fmt.Errorf("wrong task response"), 1)
}
task := results.Tasks[0]
waitSeconds := c.Int("wait-seconds")
result, err := tasks.WaitTaskAndReturnResult(client, task, stopOnTaskError, waitSeconds, infoRetriever)
if err != nil {
return cli.NewExitError(err, 1)
}
ShowResults(result, c.String("format"))
} else {
ShowResults(results, c.String("format"))
}
return nil
}
func GetAbsPath(filename string) (string, error) {
path, err := homedir.Expand(filename)
if err != nil {
return "", err
}
path, err = filepath.Abs(path)
if err != nil {
return "", err
}
return path, nil
}
func FileExists(filename string) (bool, error) {
path, err := GetAbsPath(filename)
if err != nil {
return false, err
}
info, err := os.Stat(path)
if os.IsNotExist(err) {
return false, nil
}
return !info.IsDir(), nil
}
func WriteToFile(filename string, content []byte) error {
path, err := GetAbsPath(filename)
if err != nil {
return err
}
err = os.WriteFile(path, content, 0644) // nolint
return err
}
func ReadFile(filename string) ([]byte, error) {
path, err := GetAbsPath(filename)
if err != nil {
return nil, err
}
return os.ReadFile(path)
}
func CheckYamlFile(filename string) (content []byte, err error) {
content, err = ReadFile(filename)
if err != nil {
return
}
out := make(map[string]interface{})
err = yaml.Unmarshal(content, out)
if err != nil {
return
}
return
}
func getSliceLength(slice interface{}) (int, error) {
switch t := slice.(type) {
case []string:
return len(t), nil
case []int:
return len(t), nil
}
return 0, fmt.Errorf("unexpected type: %T", slice)
}
func ValidateEqualSlicesLength(slices ...interface{}) error {
if len(slices) == 0 || len(slices) == 1 {
return nil
}
ln, err := getSliceLength(slices[0])
if err != nil {
return err
}
for _, slice := range slices[1:] {
lns, err := getSliceLength(slice)
if err != nil {
return err
}
if lns != ln {
return fmt.Errorf("element %v has different length than %v", slice, slices[0])
}
}
return nil
}
type Item interface {
}
type Result struct {
Item Item
Request string
Err error
}
// Find field name from struct by different key, example by `json`
func getStructFieldName(tag, key string, s interface{}) string {
valueG := reflect.ValueOf(s).Elem()
rt := valueG.Type()
if rt.Kind() != reflect.Struct {
panic("bad type")
}
for i := 0; i < valueG.NumField(); i++ {
f := valueG.Type().Field(i) // .Name
v := strings.Split(f.Tag.Get(key), ",")[0]
if v == tag {
return f.Name
}
}
return ""
}
// UpdateStructFromString Update struct from string by pattern key=value;
func UpdateStructFromString(item interface{}, value string) error {
entries := strings.Split(value, ";")
r := reflect.ValueOf(item)
if r.Kind() == reflect.Ptr {
r = r.Elem()
}
for _, e := range entries {
parts := strings.Split(e, "=")
if len(parts) < 2 {
panic(fmt.Sprintf("Wrong some flag key and value format, should be key=value.\n %T", value))
}
jsonName := parts[0]
fieldName := getStructFieldName(jsonName, "json", item)
field := reflect.Indirect(r).FieldByName(fieldName)
value, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return err
}
field.SetInt(value)
}
return nil
}
// StructToString Make string from struct for example or documentation
func StructToString(item interface{}) string {
valueG := reflect.ValueOf(item).Elem()
var fields []string
for i := 0; i < valueG.NumField(); i++ {
f := valueG.Type().Field(i) // .Name
jsonTag := f.Tag.Get("json")
if jsonTag == "-" || jsonTag == "" {
continue
}
name := strings.Split(jsonTag, ",")[0]
defaultName := fmt.Sprintf("%s=0", name)
fields = append(fields, defaultName)
}
result := strings.Join(fields, ";")
return result
}