Skip to content

Commit

Permalink
Output query enforces 'YML' format (when text detected)
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Jan 7, 2025
1 parent 12082c6 commit 8a7ecb4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions cmd/aem/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ func (c *CLI) onStart() {
if c.outputValue != common.OutputValueNone && c.outputValue != common.OutputValueAll {
c.outputFormat = fmtx.Text
}
if c.outputQuery != "" && c.outputFormat == fmtx.Text {
c.outputFormat = fmtx.YML
}

noColor := cv.GetBool("output.no_color")
if c.outputFormat != fmtx.Text || c.outputLogMode != cfg.OutputLogConsole {
Expand Down Expand Up @@ -299,15 +302,16 @@ func (c *CLI) printOutputDataIndented(writer *textio.PrefixWriter, value any, ke
}

func (c *CLI) printOutputMarshaled() {
if c.outputValue == common.OutputValueNone {
if c.outputQuery != "" {
c.printOutputMarshaledValue(c.queryOutputResponse())
return
}

if c.outputQuery == "" {
if c.outputValue != common.OutputValueNone {
c.printOutputMarshaledValue(c.outputResponse)
return
}
}

func (c *CLI) queryOutputResponse() any {
// JMESPath bug workaround, see: https://github.com/jmespath/go-jmespath/issues/32)
cloned, err := c.outputResponse.Clone()
if err != nil {
Expand All @@ -317,8 +321,7 @@ func (c *CLI) printOutputMarshaled() {
if err != nil {
log.Fatalf("cannot perform query '%s' on CLI output data: %s", c.outputQuery, err)
}

c.printOutputMarshaledValue(queried)
return queried
}

func (c *CLI) printOutputMarshaledValue(value any) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/aem/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c *CLI) rootFlags(cmd *cobra.Command) {
"Limits output to single variable")
_ = cv.BindPFlag("output.value", cmd.PersistentFlags().Lookup("output-value"))

cmd.PersistentFlags().String("output-format", cv.GetString("output.format"), "Controls output format ("+strings.Join(cfg.OutputFormats(), "|")+")")
cmd.PersistentFlags().StringP("output-format", "F", cv.GetString("output.format"), "Controls output format ("+strings.Join(cfg.OutputFormats(), "|")+")")
_ = cv.BindPFlag("output.format", cmd.PersistentFlags().Lookup("output-format"))

cmd.PersistentFlags().StringP("output-query", "Q", cv.GetString("output.query"), "Filters output using JMESPath query (only JSON and YML formats)")
Expand Down

0 comments on commit 8a7ecb4

Please sign in to comment.