-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_input.go
206 lines (163 loc) · 4.25 KB
/
file_input.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
// https://github.com/sl1pm4t/k2tf/blob/master/pkg/file_io/input.go
// Adapted to fit to mimir promehteus rules files.
package main
import (
"bufio"
"bytes"
"encoding/json"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/grafana/mimir/pkg/mimirtool/rules"
"github.com/rs/zerolog/log"
)
func ReadYAMLInput(input string) []rules.RuleNamespace {
if input == "-" || input == "" {
return readYAMLStdinInput(input)
}
return readYAMLFilesInput(input)
}
func readYAMLStdinInput(input string) []rules.RuleNamespace {
info, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
if info.Mode()&os.ModeCharDevice != 0 {
log.Fatal().Msg("No data read from stdin")
}
reader := bufio.NewReader(os.Stdin)
buf := &bytes.Buffer{}
buf.ReadFrom(reader)
parsed, errs := rules.ParseBytes(buf.Bytes())
if len(errs) > 0 {
log.Fatal().Err(errs[0]).Msg("Could not parse stdin")
}
return parsed
}
func readYAMLFilesInput(input string) []rules.RuleNamespace {
var objs []rules.RuleNamespace
if _, err := os.Stat(input); os.IsNotExist(err) {
log.Fatal().Str("file", input).Msg("input filepath does not exist")
}
file, err := os.Open(input)
if err != nil {
log.Fatal().Err(err).Msg("")
}
fs, err := file.Stat()
if err != nil {
log.Fatal().Err(err).Msg("")
}
readYamlFile := func(fileName string) {
log.Debug().Msgf("reading file: %s", fileName)
content, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatal().Err(err).Msg("could not read file")
}
r := bytes.NewReader(content)
buf := &bytes.Buffer{}
buf.ReadFrom(r)
obj, errs := rules.ParseBytes(buf.Bytes())
if len(errs) > 0 {
log.Warn().Err(errs[0]).Msgf("could not parse file %s", fileName)
}
objs = append(objs, obj...)
}
if fs.Mode().IsDir() {
// read directory
log.Debug().Msgf("reading directory: %s", input)
dirContents, err := file.Readdirnames(0)
if err != nil {
log.Fatal().Err(err).Msg("")
}
for _, f := range dirContents {
if strings.HasSuffix(f, ".yml") || strings.HasSuffix(f, ".yaml") {
readYamlFile(filepath.Join(input, f))
}
}
} else {
// read single file
readYamlFile(input)
}
return objs
}
func ReadHCLInput(input string) []map[string]interface{} {
if input == "-" || input == "" {
return readHCLStdinInput(input)
}
return readHCLFilesInput(input)
}
func readHCLStdinInput(input string) []map[string]interface{} {
info, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
if info.Mode()&os.ModeCharDevice != 0 {
log.Fatal().Msg("No data read from stdin")
}
buffer := bytes.NewBuffer([]byte{})
var stream io.Reader
_, err = buffer.ReadFrom(stream)
dataBytes, err := Bytes(buffer.Bytes(), "STDIN")
if err != nil {
log.Fatal().Err(err).Msg("Could not parse stdin")
}
var data map[string]interface{}
err = json.Unmarshal(dataBytes, &data)
if err != nil {
log.Warn().Err(err).Msgf("could not unmarshal")
}
return []map[string]interface{}{data}
}
func readHCLFilesInput(input string) []map[string]interface{} {
var objs []map[string]interface{}
if _, err := os.Stat(input); os.IsNotExist(err) {
log.Fatal().Str("file", input).Msg("input filepath does not exist")
}
file, err := os.Open(input)
if err != nil {
log.Fatal().Err(err).Msg("")
}
fs, err := file.Stat()
if err != nil {
log.Fatal().Err(err).Msg("")
}
readHCLFile := func(fileName string) {
log.Debug().Msgf("reading file: %s", fileName)
content, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatal().Err(err).Msg("could not read file")
}
r := bytes.NewReader(content)
buf := &bytes.Buffer{}
buf.ReadFrom(r)
dataBytes, err := Bytes(buf.Bytes(), "STDIN")
if err != nil {
log.Warn().Err(err).Msgf("could not parse file %s", fileName)
}
var obj map[string]interface{}
err = json.Unmarshal(dataBytes, &obj)
if err != nil {
log.Warn().Err(err).Msgf("could not unmarshal file %s", fileName)
}
objs = append(objs, obj)
}
if fs.Mode().IsDir() {
// read directory
log.Debug().Msgf("reading directory: %s", input)
dirContents, err := file.Readdirnames(0)
if err != nil {
log.Fatal().Err(err).Msg("")
}
for _, f := range dirContents {
if strings.HasSuffix(f, ".tf") {
readHCLFile(filepath.Join(input, f))
}
}
} else {
// read single file
readHCLFile(input)
}
return objs
}