Skip to content

Commit

Permalink
jpp: Add --slurp, -s bool flag like jq has
Browse files Browse the repository at this point in the history
Read one or more input JSON objects into an array and apply the
JMESPath expression to the resulting array.
  • Loading branch information
zmedico committed Jun 15, 2021
1 parent d75cf88 commit 8a08979
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion cmd/jpp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func main() {
Name: "expr-file, e",
Usage: "Read JMESPath expression from the specified file.",
},
cli.BoolFlag{
Name: "slurp, s",
Usage: "Read one or more input JSON objects into an array and apply the JMESPath expression to the resulting array.",
},
cli.BoolFlag{
Name: "unquoted, u",
Usage: "If the final result is a string, it will be printed without quotes.",
Expand Down Expand Up @@ -97,9 +101,26 @@ func runMain(c *cli.Context) int {
} else {
jsonParser = json.NewDecoder(os.Stdin)
}

var slurpInput []interface{}
if c.Bool("slurp") {
for {
var element interface{}
if err := jsonParser.Decode(&element); err == io.EOF {
break
} else if err != nil {
errMsg("Error parsing input json: %s\n", err)
return 2
}
slurpInput = append(slurpInput, element)
}
}

for {
var input interface{}
if err := jsonParser.Decode(&input); err == io.EOF {
if c.Bool("slurp") {
input = slurpInput
} else if err := jsonParser.Decode(&input); err == io.EOF {
break
} else if err != nil {
errMsg("Error parsing input json: %s\n", err)
Expand Down Expand Up @@ -131,6 +152,9 @@ func runMain(c *cli.Context) int {
os.Stdout.Write(toJSON)
}
os.Stdout.WriteString("\n")
if c.Bool("slurp") {
break
}
}
return 0
}

0 comments on commit 8a08979

Please sign in to comment.