Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#196 Add JMESPath output querying #214

Merged
merged 10 commits into from
Jan 16, 2024
Prev Previous commit
Next Next commit
JMESPath perf optimization
  • Loading branch information
krystian-panek-vmltech committed Jan 16, 2024
commit bf582a54592b3ec2ce8b67d7794a29dd6ec368ed
25 changes: 17 additions & 8 deletions cmd/aem/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,31 +302,40 @@ func (c *CLI) printOutputMarshaled() {
if c.outputValue == common.OutputValueNone {
return
}
// Due to bug in JMESPath we need to clone response data using JSON serialization.
// Ref.: https://github.com/jmespath/go-jmespath/issues/32
outputResponse, err := c.outputResponse.Clone()

if c.outputQuery == "" {
c.printOutputMarshaledValue(c.outputResponse)
return
}

// JMESPath bug workaround, see: https://github.com/jmespath/go-jmespath/issues/32)
cloned, err := c.outputResponse.Clone()
if err != nil {
log.Fatalf("cannot clone CLI output data: %s", err)
}
outputQueried, err := jmespath.Search(c.outputQuery, outputResponse)
queried, err := jmespath.Search(c.outputQuery, cloned)
if err != nil {
log.Fatalf("cannot perform query '%s' on CLI output data: %s", c.outputQuery, err)
}

c.printOutputMarshaledValue(queried)
}

func (c *CLI) printOutputMarshaledValue(value any) {
switch c.outputFormat {
case fmtx.JSON:
jsonQueried, err := fmtx.MarshalJSON(outputQueried)
jsonValue, err := fmtx.MarshalJSON(value)
if err != nil {
log.Fatalf("cannot serialize CLI output to target JSON format!")
}
fmt.Println(jsonQueried)
fmt.Println(jsonValue)
break
case fmtx.YML:
ymlQueried, err := fmtx.MarshalYML(outputQueried)
ymlValue, err := fmtx.MarshalYML(value)
if err != nil {
log.Fatalf("cannot serialize CLI output to target YML format!")
}
fmt.Println(ymlQueried)
fmt.Println(ymlValue)
break
default:
log.Fatalf("cannot serialize CLI output to unsupported format '%s'", c.outputFormat)
Expand Down
Loading