-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.go
88 lines (80 loc) · 1.5 KB
/
import.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
package main
import (
"bytes"
"encoding/json"
"errors"
"io"
"os"
"github.com/rs/zerolog/log"
yaml "gopkg.in/yaml.v3"
)
func importObjects() ([]interface{}, error) {
var olist []interface{}
var err error
for _, fn := range args.Inputs {
var buf []byte
if fn == "-" {
buf, err = io.ReadAll(os.Stdin)
if err != nil {
return nil, err
}
olist, err = appendEntry(olist, buf)
if err != nil {
return nil, err
}
} else {
loads, err := load(fn, false)
if err != nil {
return nil, err
}
for _, e := range loads {
olist, err = appendEntry(olist, []byte(e.data))
if err != nil {
return nil, err
}
}
}
}
return olist, nil
}
func appendEntry(olist []interface{}, buf []byte) ([]interface{}, error) {
var err error
if len(buf) == 0 {
return olist, nil
}
if buf[0] == '[' || buf[0] == '{' {
var o interface{}
err = json.Unmarshal(buf, &o)
if err != nil {
log.Error().Msgf("error parsing json input: %v", err)
os.Exit(1)
}
if args.Split {
if list, ok := interfaceSlice(o); ok {
olist = append(olist, list...)
} else {
olist = append(olist, o)
}
} else {
olist = append(olist, o)
}
return olist, nil
}
dec := yaml.NewDecoder(bytes.NewBuffer(buf))
for {
var o interface{}
err = dec.Decode(&o)
if err == nil {
olist = append(olist, o)
continue
}
if errors.Is(err, io.EOF) {
break
}
if err != nil {
log.Error().Msgf("error parsing yaml input: %v", err)
os.Exit(1)
}
}
return olist, nil
}