/
cli.go
341 lines (288 loc) · 8.55 KB
/
cli.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
package cli
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"github.com/VojtechVitek/yaml-cli"
"github.com/pkg/errors"
flag "github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
yamlv3 "gopkg.in/yaml.v3"
)
var (
// TODO: Move these vars to a struct, they shouldn't be global. A left-over from "main" pkg.
flags = flag.NewFlagSet("yaml", flag.ExitOnError)
from = flags.String("from", "yaml", "input data format [json]")
to = flags.String("to", "yaml", "output data format [json]")
ignoreMissing = flags.Bool("ignore-missing", false, "yaml get: ignore missing nodes")
printKey = flags.Bool("print-key", false, "yaml get: print node key in front of the value, so the output is valid YAML")
noSeparator = flags.Bool("no-separator", false, "yaml get: don't print `---' separator between YAML documents")
invert = flags.BoolP("invert-match", "v", false, "yaml grep -v: select non-matching documents")
)
// TODO: Split into multiple files/functions. This function grew too much
// over time while adding new commands and functionality.
func Run(out io.Writer, in io.Reader, args []string) error {
var b bytes.Buffer
flags.Parse(args)
switch *from {
case "yaml", "yml": // Nop.
case "json":
in = &b
if err := jsonToYAML(&b, os.Stdin); err != nil {
log.Fatal(errors.Wrap(err, "failed to convert output to JSON"))
}
default:
return errors.Errorf("unknown --from format %q", *to)
}
switch *to {
case "yaml", "yml": // Nop.
case "json":
out = &b
default:
return errors.Errorf("unknown --to format %q", *to)
}
if err := run(out, in, flags.Args()); err != nil {
return errors.Wrap(err, "failed to run")
}
if *to == "json" {
if err := yamlToJSON(os.Stdout, &b); err != nil {
return errors.Wrap(err, "failed to convert output to JSON")
}
}
return nil
}
func run(out io.Writer, in io.Reader, args []string) error {
if len(args) == 1 && args[0] == "count" {
count := 0
dec := yamlv3.NewDecoder(in)
for { // For all YAML documents in STDIN.
var doc yamlv3.Node
if err := dec.Decode(&doc); err != nil {
if err == io.EOF { // Last document.
break
}
return errors.Wrap(err, "failed to decode YAML document(s) from stdin")
}
count++
}
fmt.Println(count)
return nil
}
if len(args) <= 1 {
return errors.New(`usage: see https://github.com/VojtechVitek/yaml-cli/blob/master/README.md`)
}
enc := yamlv3.NewEncoder(out)
enc.SetIndent(2)
if args[0] == "cat" {
for _, filename := range args[1:] {
f, err := os.Open(filename)
if err != nil {
return errors.Wrap(err, "failed to open file")
}
dec := yamlv3.NewDecoder(f)
for { // For all YAML documents in the file (they might be separated by '---')
var doc yamlv3.Node
if err := dec.Decode(&doc); err != nil {
if err == io.EOF { // Last document.
break
}
return errors.Wrapf(err, "failed to decode file %v", filename)
}
if err := enc.Encode(&doc); err != nil {
return errors.Wrap(err, "failed to encode YAML node")
}
}
if err := f.Close(); err != nil {
return errors.Wrap(err, "failed to close file")
}
}
return nil
}
var tfs []*yaml.Transformation
switch args[0] {
case "apply":
filenames := args[1:]
fileTfs := make([][]*yaml.Transformation, len(filenames))
var g errgroup.Group
for i, filename := range filenames {
i, filename := i, filename // local copy for goroutine
g.Go(func() error {
f, err := os.Open(filename)
if err != nil {
return errors.Wrapf(err, "failed to read transformation %v", filename)
}
fileTfs[i], err = yaml.Transformations(f)
if err != nil {
return errors.Wrapf(err, "failed to parse transformation %v", filename)
}
return f.Close()
})
}
if err := g.Wait(); err != nil {
return err
}
for _, tf := range fileTfs {
tfs = append(tfs, tf...)
}
// do not return
case "doc":
if len(args) != 2 {
return errors.New("usage: yaml doc $index")
}
index, err := strconv.Atoi(args[1])
if err != nil {
return errors.Wrapf(err, "yaml doc $index: failed to parse $index %q", args[1])
}
count := 0
dec := yamlv3.NewDecoder(in)
for { // For all YAML documents in STDIN.
var doc yamlv3.Node
if err := dec.Decode(&doc); err != nil {
if err == io.EOF { // Last document.
break
}
return errors.Wrap(err, "failed to decode YAML document(s) from stdin")
}
if count == index {
if err := enc.Encode(&doc); err != nil {
return errors.Wrap(err, "failed to encode YAML node")
}
io.Copy(ioutil.Discard, in)
return nil
}
count++
}
return errors.Errorf("doc %v not found (count=%v)", index, count)
}
dec := yamlv3.NewDecoder(in)
for { // For all YAML documents in STDIN.
var doc yamlv3.Node
if err := dec.Decode(&doc); err != nil {
if err == io.EOF { // Last document.
return nil
}
return errors.Wrap(err, "failed to decode YAML document(s) from stdin")
}
switch args[0] {
case "apply":
for _, tf := range tfs {
if err := tf.Apply(&doc); err != nil {
return errors.Wrapf(err, "failed to apply transformation")
}
}
if err := enc.Encode(&doc); err != nil {
return errors.Wrap(err, "failed to encode YAML node")
}
case "grep":
selectors := args[1:]
tfs, err := yaml.Transformations(strings.NewReader(fmt.Sprintf("match:\n %v", strings.Join(selectors, "\n "))))
if err != nil {
return errors.Wrapf(err, "failed to parse grep `key: value' pairs from %q", selectors)
}
tf := tfs[0]
ok, _ := tf.MustMatchAll(&doc)
//fmt.Fprintf(os.Stderr, "MustMatchAll: %v\n", err)
if ok != *invert { // match
if err := enc.Encode(&doc); err != nil {
return errors.Wrap(err, "failed to encode YAML node")
}
}
case "len":
selector := args[1]
nodes, err := yaml.Get(&doc, strings.Split(selector, "."))
if err != nil {
return errors.Wrapf(err, "failed to get %q", selector)
}
for _, node := range nodes {
fmt.Println(len(node.Content))
}
return nil
case "get":
useTopLevelEnc := true
var allMatchedNodes []*yamlv3.Node
for _, selector := range args[1:] {
selectors := strings.Split(selector, ".")
lastSelector := selectors[len(selectors)-1]
nodes, err := yaml.Get(&doc, selectors)
if err != nil {
if !*ignoreMissing {
return errors.Wrapf(err, "failed to get %q", selector)
}
}
for _, node := range nodes {
if *printKey {
node = &yamlv3.Node{
Kind: yamlv3.MappingNode,
Tag: "!!map",
Content: []*yamlv3.Node{
&yamlv3.Node{
Kind: yamlv3.ScalarNode,
Value: lastSelector,
},
node,
},
}
}
allMatchedNodes = append(allMatchedNodes, node)
}
}
for _, node := range allMatchedNodes {
if useTopLevelEnc {
useTopLevelEnc = false
// Top level enc will print separator between documents.
if err := enc.Encode(node); err != nil {
return errors.Wrap(err, "failed to encode YAML node")
}
} else {
// Omit separator between one YAML document's nodes.
enc := yamlv3.NewEncoder(out)
enc.SetIndent(2)
if err := enc.Encode(node); err != nil {
return errors.Wrap(err, "failed to encode YAML node")
}
}
}
case "set":
keyValues := args[1:]
tfs, err := yaml.Transformations(strings.NewReader(fmt.Sprintf("set:\n %v", strings.Join(keyValues, "\n "))))
if err != nil {
return errors.Wrapf(err, "failed to parse `key: value' pairs from %v", keyValues)
}
tf := tfs[0]
if err := tf.Apply(&doc); err != nil {
return errors.Wrapf(err, "failed to set %v", tf.Sets)
}
if err := enc.Encode(&doc); err != nil {
return errors.Wrap(err, "failed to encode YAML node")
}
case "default":
keyValues := args[1:]
tfs, err := yaml.Transformations(strings.NewReader(fmt.Sprintf("default:\n %v", strings.Join(keyValues, "\n "))))
if err != nil {
return errors.Wrapf(err, "failed to parse `key: value' pairs from %v", keyValues)
}
tf := tfs[0]
if err := tf.Apply(&doc); err != nil {
return errors.Wrapf(err, "failed to set default %v", tf.Defaults)
}
if err := enc.Encode(&doc); err != nil {
return errors.Wrap(err, "failed to encode YAML node")
}
case "delete":
selector := args[1]
if err := yaml.Delete(&doc, selector); err != nil {
if false { // TODO: --strict mode, where we'd error out on non-existent selectors?
return errors.Wrapf(err, "failed to delete %q", selector)
}
}
default:
return errors.Errorf("%v: unknown command", args[0])
}
}
return nil
}