forked from jmespath/jp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jp.go
156 lines (146 loc) · 3.55 KB
/
jp.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/jmespath/jp/Godeps/_workspace/src/github.com/codegangsta/cli"
"github.com/jmespath/jp/Godeps/_workspace/src/github.com/jmespath/go-jmespath"
)
const version = "0.1.3"
func main() {
app := cli.NewApp()
app.Name = "jp"
app.Version = version
app.Usage = "jp [<options>] <expression>"
app.Author = ""
app.Email = ""
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "filename, f",
Usage: "Read input JSON from a file instead of stdin.",
},
cli.StringFlag{
Name: "expr-file, e",
Usage: "Read JMESPath expression from the specified file.",
},
cli.BoolFlag{
Name: "unquoted, u",
Usage: "If the final result is a string, it will be printed without quotes.",
EnvVar: "JP_UNQUOTED",
},
cli.BoolFlag{
Name: "ast",
Usage: "Only print the AST of the parsed expression. Do not rely on this output, only useful for debugging purposes.",
},
cli.BoolFlag{
Name: "no-null",
Usage: "Omit null outputs.",
},
cli.BoolFlag{
Name: "pretty",
Usage: "Output indented JSON",
},
cli.IntFlag{
Name: "buffer-size",
Usage: "Size of input buffer",
},
}
app.Action = runMainAndExit
app.Run(os.Args)
}
func runMainAndExit(c *cli.Context) {
os.Exit(runMain(c))
}
func errMsg(msg string, a ...interface{}) int {
fmt.Fprintf(os.Stderr, msg, a...)
fmt.Fprintln(os.Stderr)
return 1
}
func runMain(c *cli.Context) int {
var expression string
if c.String("expr-file") != "" {
byteExpr, err := ioutil.ReadFile(c.String("expr-file"))
expression = string(byteExpr)
if err != nil {
return errMsg("Error opening expression file: %s", err)
}
} else {
if len(c.Args()) == 0 {
return errMsg("Must provide at least one argument.")
}
expression = c.Args()[0]
}
if c.Bool("ast") {
parser := jmespath.NewParser()
parsed, err := parser.Parse(expression)
if err != nil {
if syntaxError, ok := err.(jmespath.SyntaxError); ok {
return errMsg("%s\n%s\n",
syntaxError,
syntaxError.HighlightLocation())
}
return errMsg("%s", err)
}
fmt.Println("")
fmt.Printf("%s\n", parsed)
return 0
}
var jsonParser *json.Decoder
if c.String("filename") != "" {
f, err := os.Open(c.String("filename"))
if err != nil {
return errMsg("Error opening input file: %s", err)
}
jsonParser = json.NewDecoder(f)
} else {
jsonParser = json.NewDecoder(os.Stdin)
}
bufferSize := c.Int("buffer-size")
if bufferSize == 0 {
bufferSize = 1
}
inputs := make(chan interface{}, 1)
go func() {
for jsonParser.More() {
var input interface{}
if err := jsonParser.Decode(&input); err != nil {
panic(fmt.Errorf("Error parsing input json: %s\n", err))
}
inputs <- input
}
close(inputs)
}()
for input := range inputs {
result, err := jmespath.Search(expression, input)
if err != nil {
if syntaxError, ok := err.(jmespath.SyntaxError); ok {
return errMsg("%s\n%s\n",
syntaxError,
syntaxError.HighlightLocation())
}
return errMsg("Error evaluating JMESPath expression: %s", err)
}
if result != nil || !c.Bool("no-null") {
converted, isString := result.(string)
if c.Bool("unquoted") && isString {
os.Stdout.WriteString(converted)
} else {
var toJSON []byte
var err error
if c.Bool("pretty") {
toJSON, err = json.MarshalIndent(result, "", " ")
} else {
toJSON, err = json.Marshal(result)
}
if err != nil {
errMsg("Error marshalling result to JSON: %s\n", err)
return 3
}
os.Stdout.Write(toJSON)
}
os.Stdout.WriteString("\n")
}
}
return 0
}