Skip to content

Commit 8a7ecb4

Browse files
Output query enforces 'YML' format (when text detected)
1 parent 12082c6 commit 8a7ecb4

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

cmd/aem/cli.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ func (c *CLI) onStart() {
115115
if c.outputValue != common.OutputValueNone && c.outputValue != common.OutputValueAll {
116116
c.outputFormat = fmtx.Text
117117
}
118+
if c.outputQuery != "" && c.outputFormat == fmtx.Text {
119+
c.outputFormat = fmtx.YML
120+
}
118121

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

301304
func (c *CLI) printOutputMarshaled() {
302-
if c.outputValue == common.OutputValueNone {
305+
if c.outputQuery != "" {
306+
c.printOutputMarshaledValue(c.queryOutputResponse())
303307
return
304308
}
305-
306-
if c.outputQuery == "" {
309+
if c.outputValue != common.OutputValueNone {
307310
c.printOutputMarshaledValue(c.outputResponse)
308-
return
309311
}
312+
}
310313

314+
func (c *CLI) queryOutputResponse() any {
311315
// JMESPath bug workaround, see: https://github.com/jmespath/go-jmespath/issues/32)
312316
cloned, err := c.outputResponse.Clone()
313317
if err != nil {
@@ -317,8 +321,7 @@ func (c *CLI) printOutputMarshaled() {
317321
if err != nil {
318322
log.Fatalf("cannot perform query '%s' on CLI output data: %s", c.outputQuery, err)
319323
}
320-
321-
c.printOutputMarshaledValue(queried)
324+
return queried
322325
}
323326

324327
func (c *CLI) printOutputMarshaledValue(value any) {

cmd/aem/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (c *CLI) rootFlags(cmd *cobra.Command) {
5656
"Limits output to single variable")
5757
_ = cv.BindPFlag("output.value", cmd.PersistentFlags().Lookup("output-value"))
5858

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

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

0 commit comments

Comments
 (0)