-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (65 loc) · 1.29 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"strings"
)
var (
nl = []byte{'\n'}
quote = []byte{'"'}
)
func main() {
flag.Usage = func() {
fmt.Fprintln(os.Stderr, strings.Join([]string{
"Flatten JSON into greppable list",
"",
"Usage:",
" fj [OPTIONS] [FILE|-]",
"",
"Options:",
" -u, --unflatten Turn flattened list back into JSON",
" -s, --stream Treat JSON/flattened as stream",
" -h, --help Print this information",
}, "\n"))
}
var (
unflattenFlag bool
streamFlag bool
)
flag.BoolVar(&unflattenFlag, "u", false, "")
flag.BoolVar(&unflattenFlag, "unflatten", false, "")
flag.BoolVar(&streamFlag, "s", false, "")
flag.BoolVar(&streamFlag, "stream", false, "")
flag.Parse()
ins := make([]io.Reader, 0, flag.NArg())
for _, file := range flag.Args() {
if file == "-" {
ins = append(ins, os.Stdin)
} else {
f, err := os.Open(file)
fail(err)
ins = append(ins, f)
}
}
if len(ins) == 0 {
ins = append(ins, os.Stdin)
}
in := io.MultiReader(ins...)
out := bufio.NewWriter(os.Stdout)
if unflattenFlag {
unflatten(in, out, streamFlag)
} else {
flatten(in, out, streamFlag)
}
fail(out.Flush())
}
func fail(err error) {
if err == nil {
return
}
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}