forked from gonvenience/ytbx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
352 lines (289 loc) · 10.1 KB
/
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
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
// Copyright © 2018 The Homeport Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package ytbx
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
"github.com/BurntSushi/toml"
"github.com/gonvenience/bunt"
"github.com/gonvenience/text"
ordered "github.com/virtuald/go-ordered-json"
yamlv3 "gopkg.in/yaml.v3"
)
// PreserveKeyOrderInJSON specifies whether a special library is used to decode
// JSON input to preserve the order of keys in maps even though that is not part
// of the JSON specification.
var PreserveKeyOrderInJSON = false
// DecoderProxy can either be used with the standard JSON Decoder, or the
// specialised JSON library fork that supports preserving key order
type DecoderProxy struct {
standard *json.Decoder
ordered *ordered.Decoder
}
// InputFile represents the actual input file (local, or fetched remotely) that
// needs to be processed. It can contain multiple documents, where a document
// is a map or a list of things.
type InputFile struct {
Location string
Note string
Documents []*yamlv3.Node
Names []string
}
// NewDecoderProxy creates a new decoder proxy which either works in ordered
// mode or standard mode.
func NewDecoderProxy(keepOrder bool, r io.Reader) *DecoderProxy {
if keepOrder {
decoder := ordered.NewDecoder(r)
decoder.UseOrderedObject()
return &DecoderProxy{ordered: decoder}
}
return &DecoderProxy{standard: json.NewDecoder(r)}
}
// Decode is a delegate function that calls JSON Decoder `Decode`
func (d *DecoderProxy) Decode(v interface{}) error {
if d.ordered != nil {
return d.ordered.Decode(v)
}
return d.standard.Decode(v)
}
// HumanReadableLocationInformation create a nicely decorated information about
// the provided input location. It will output the absolute path of the file
// rather than the possibly relative location, or it will show the URL in the
// usual look-and-feel of URIs.
func HumanReadableLocationInformation(inputFile InputFile) string {
var buf bytes.Buffer
// Start with a nice location output
buf.WriteString(HumanReadableLocation(inputFile.Location))
// Add additional note if it is set
if inputFile.Note != "" {
bunt.Fprintf(&buf, ", Orange{%s}", inputFile.Note)
}
// Add an information about how many documents are in the provided input file
if documents := len(inputFile.Documents); documents > 1 {
bunt.Fprintf(&buf, ", Aquamarine{*%s*}", text.Plural(documents, "document"))
}
return buf.String()
}
// HumanReadableLocation returns a human readable location with proper coloring
func HumanReadableLocation(location string) string {
if IsStdin(location) {
return bunt.Sprint("_*stdin*_")
}
if _, err := os.Stat(location); err == nil {
return bunt.Sprintf("*%s*", location)
}
if _, err := url.ParseRequestURI(location); err == nil {
return bunt.Sprintf("CornflowerBlue{~%s~}", location)
}
return location
}
// LoadFiles concurrently loads two files from the provided locations
func LoadFiles(locationA string, locationB string) (InputFile, InputFile, error) {
type resultPair struct {
result InputFile
err error
}
fromChan := make(chan resultPair, 1)
toChan := make(chan resultPair, 1)
go func() {
result, err := LoadFile(locationA)
fromChan <- resultPair{result, err}
}()
go func() {
result, err := LoadFile(locationB)
toChan <- resultPair{result, err}
}()
from := <-fromChan
if from.err != nil {
return InputFile{}, InputFile{}, from.err
}
to := <-toChan
if to.err != nil {
return InputFile{}, InputFile{}, to.err
}
return from.result, to.result, nil
}
// LoadFile processes the provided input location to load it as one of the
// supported document formats, or plain text if nothing else works.
func LoadFile(location string) (InputFile, error) {
if info, err := os.Stat(location); err == nil && info.IsDir() {
return LoadDirectory(location)
}
var (
documents []*yamlv3.Node
data []byte
err error
)
if data, err = getBytesFromLocation(location); err != nil {
return InputFile{}, fmt.Errorf("unable to load data from %s: %w", HumanReadableLocation(location), err)
}
if documents, err = LoadDocuments(data); err != nil {
return InputFile{}, fmt.Errorf("unable to parse data from %s: %w", HumanReadableLocation(location), err)
}
return InputFile{
Location: location,
Documents: documents,
}, nil
}
// LoadDirectory reads the provided location as a directory and processes all
// files in the directory as documents
func LoadDirectory(location string) (InputFile, error) {
files, err := os.ReadDir(location)
if err != nil {
return InputFile{}, fmt.Errorf("failed to read files in directory %s: %w", location, err)
}
sort.Slice(files, func(i, j int) bool {
return strings.Compare(files[i].Name(), files[j].Name()) < 0
})
var result = InputFile{
Location: location,
}
for _, file := range files {
bytes, err := getBytesFromLocation(filepath.Join(location, file.Name()))
if err != nil {
return InputFile{}, err
}
docs, err := LoadDocuments(bytes)
if err != nil {
return InputFile{}, fmt.Errorf("failed to read %s: %w", filepath.Join(location, file.Name()), err)
}
result.Documents = append(result.Documents, docs...)
result.Names = append(result.Names, file.Name())
}
return result, nil
}
// LoadDocuments reads the provided input data slice as a YAML, JSON, or TOML
// file with potential multiple documents. It only acts as a dispatcher and
// depending on the input will either use `LoadTOMLDocuments`,
// `LoadJSONDocuments`, or `LoadYAMLDocuments`.
func LoadDocuments(input []byte) ([]*yamlv3.Node, error) {
// There is no easy check whether the input data is TOML format, this is
// why there is currently no other option than simply trying to parse it.
if toml, err := LoadTOMLDocuments(input); err == nil {
return toml, err
}
// In case the input data set starts with either a map or list start
// symbol, it is assumed to be a JSON document. In any other case, use
// the YAML parser function which also covers plain text documents.
switch input[0] {
case '{', '[':
return LoadJSONDocuments(input)
default:
return LoadYAMLDocuments(input)
}
}
// LoadJSONDocuments reads the provided input data slice as a JSON file with
// potential multiple documents. Each document in the JSON stream results in an
// entry of the result slice.
func LoadJSONDocuments(input []byte) ([]*yamlv3.Node, error) {
values := []*yamlv3.Node{}
decoder := NewDecoderProxy(PreserveKeyOrderInJSON, bytes.NewReader(input))
for {
var value interface{}
err := decoder.Decode(&value)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
node, err := asYAMLNode(value)
if err != nil {
return nil, err
}
values = append(values, node)
}
return values, nil
}
// LoadYAMLDocuments reads the provided input data slice as a YAML file with
// potential multiple documents. Each document in the YAML stream results in an
// entry of the result slice.
func LoadYAMLDocuments(input []byte) ([]*yamlv3.Node, error) {
documents := []*yamlv3.Node{}
decoder := yamlv3.NewDecoder(bytes.NewReader(input))
for {
var node yamlv3.Node
err := decoder.Decode(&node)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
documents = append(documents, &node)
}
return documents, nil
}
// LoadTOMLDocuments reads the provided input data slice as a TOML file, which
// can only have one document. For the sake of having similar sounding
// functions and the same signatures, the function uses the plural in its name
// and returns a list of results even though it will only contain one entry.
// All map entries inside the result document are converted into Go-YAMLv3 Node
// types to make it compatible with the rest of the package.
func LoadTOMLDocuments(input []byte) ([]*yamlv3.Node, error) {
var data interface{}
if err := toml.Unmarshal(input, &data); err != nil {
return nil, err
}
node, err := asYAMLNode(data)
if err != nil {
return nil, err
}
return []*yamlv3.Node{node}, nil
}
func getBytesFromLocation(location string) ([]byte, error) {
// Handle special location "-" which refers to STDIN stream
if IsStdin(location) {
return io.ReadAll(os.Stdin)
}
// Handle location as local file if there is a file at that location
if _, err := os.Stat(location); err == nil {
return os.ReadFile(location)
}
// Handle location as a URI if it looks like one
if _, err := url.ParseRequestURI(location); err == nil {
response, err := http.Get(location)
if err != nil {
return nil, err
}
defer response.Body.Close()
data, err := io.ReadAll(response.Body)
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to retrieve data from location %s: %s", location, string(data))
}
return data, err
}
// In any other case, bail out ...
return nil, fmt.Errorf("unable to get any content using location %s: it is not a file or usable URI", location)
}
// IsStdin checks whether the provided input location refers to the dash
// character which usually serves as the replacement to point to STDIN rather
// than a file.
func IsStdin(location string) bool {
return strings.TrimSpace(location) == "-"
}