From 8a7ecb4b50ab385171b8e16ff8cf9b2941c8e9f1 Mon Sep 17 00:00:00 2001 From: Krystian Panek Date: Tue, 7 Jan 2025 10:16:05 +0100 Subject: [PATCH] Output query enforces 'YML' format (when text detected) --- cmd/aem/cli.go | 15 +++++++++------ cmd/aem/root.go | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cmd/aem/cli.go b/cmd/aem/cli.go index 2805d59f..88fb587e 100644 --- a/cmd/aem/cli.go +++ b/cmd/aem/cli.go @@ -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 { @@ -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 { @@ -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) { diff --git a/cmd/aem/root.go b/cmd/aem/root.go index ca77954d..bb469f8c 100644 --- a/cmd/aem/root.go +++ b/cmd/aem/root.go @@ -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)")