Skip to content

Commit

Permalink
Merge pull request #214 from wttech/196-jmespath-integration
Browse files Browse the repository at this point in the history
#196 Add JMESPath output querying
  • Loading branch information
krystian-panek-vmltech authored Jan 16, 2024
2 parents 751b8e2 + 7f42e6c commit c64affc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
51 changes: 45 additions & 6 deletions cmd/aem/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"github.com/fatih/color"
"github.com/iancoleman/strcase"
"github.com/jmespath-community/go-jmespath"
"github.com/samber/lo"
"github.com/segmentio/textio"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -45,6 +47,7 @@ type CLI struct {
outputLogFile string
outputLogMode string
outputResponse *OutputResponse
outputQuery string
outputWriter io.Writer
outputNoColor bool

Expand Down Expand Up @@ -105,6 +108,7 @@ func (c *CLI) onStart() {
c.outputResponse = outputResponseDefault()
c.outputValue = cv.GetString("output.value")
c.outputFormat = strings.ReplaceAll(cv.GetString("output.format"), "yaml", "yml")
c.outputQuery = cv.GetString("output.query")
c.outputLogFile = cv.GetString("output.log.file")
c.outputLogMode = cv.GetString("output.log.mode")

Expand Down Expand Up @@ -295,26 +299,61 @@ func (c *CLI) printOutputDataIndented(writer *textio.PrefixWriter, value any, ke
}

func (c *CLI) printOutputMarshaled() {
if c.outputValue == common.OutputValueNone {
return
}

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)
}
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:
json, err := fmtx.MarshalJSON(c.outputResponse)
jsonValue, err := fmtx.MarshalJSON(value)
if err != nil {
log.Fatalf("cannot serialize CLI output to to target JSON format!")
log.Fatalf("cannot serialize CLI output to target JSON format!")
}
fmt.Println(json)
fmt.Println(jsonValue)
break
case fmtx.YML:
yml, err := fmtx.MarshalYML(c.outputResponse)
ymlValue, err := fmtx.MarshalYML(value)
if err != nil {
log.Fatalf("cannot serialize CLI output to to target YML format!")
log.Fatalf("cannot serialize CLI output to target YML format!")
}
fmt.Println(yml)
fmt.Println(ymlValue)
break
default:
log.Fatalf("cannot serialize CLI output to unsupported format '%s'", c.outputFormat)
}
}

func (c *OutputResponse) Clone() (*OutputResponse, error) {
var clone OutputResponse
marshalled, err := json.Marshal(c)
if err != nil {
return nil, err
}
if err = json.Unmarshal(marshalled, &clone); err != nil {
return nil, err
}
return &clone, nil
}

func (c *CLI) Ok(message string) {
c.Success(message, false)
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/aem/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (c *CLI) rootFlags(cmd *cobra.Command) {
cmd.PersistentFlags().String("output-format", cv.GetString("output.format"), "Controls output format ("+strings.Join(cfg.OutputFormats(), "|")+")")
_ = cv.BindPFlag("output.format", cmd.PersistentFlags().Lookup("output-format"))

cmd.PersistentFlags().String("output-query", cv.GetString("output.query"), "Filters output using JMESPath query (only JSON and YML formats)")
_ = cv.BindPFlag("output.query", cmd.PersistentFlags().Lookup("output-query"))

cmd.PersistentFlags().String("output-log-file", cv.GetString("output.log.file"), "Controls output file path")
_ = cv.BindPFlag("output.log.file", cmd.PersistentFlags().Lookup("output-log-file"))

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/hashicorp/go-version v1.6.0
github.com/iancoleman/strcase v0.3.0
github.com/jmespath-community/go-jmespath v1.1.1
github.com/joho/godotenv v1.5.1
github.com/magiconair/properties v1.8.7
github.com/olekukonko/tablewriter v0.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jmespath-community/go-jmespath v1.1.1 h1:bFikPhsi/FdmlZhVgSCd2jj1e7G/rw+zyQfyg5UF+L4=
github.com/jmespath-community/go-jmespath v1.1.1/go.mod h1:4gOyFJsR/Gk+05RgTKYrifT7tBPWD8Lubtb5jRrfy9I=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
Expand Down
1 change: 1 addition & 0 deletions pkg/cfg/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (c *Config) setDefaults() {
v.SetDefault("output.value", common.OutputValueAll)
v.SetDefault("output.log.file", common.LogFile)
v.SetDefault("output.log.mode", OutputLogConsole)
v.SetDefault("output.query", "")

v.SetDefault("java.home_dir", "")
v.SetDefault("java.version_constraints", ">= 11, < 12")
Expand Down

0 comments on commit c64affc

Please sign in to comment.