From 992f45be373f20df282eca92d3c03ca3e7b93b76 Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Mon, 10 Jun 2024 16:00:58 +0200 Subject: [PATCH] fix(set-session): only write json output if user provides --output json --- pkg/cmd/sessions/set/set.manual.go | 2 +- pkg/config/cliConfiguration.go | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/sessions/set/set.manual.go b/pkg/cmd/sessions/set/set.manual.go index 338d7f90d..26976a509 100644 --- a/pkg/cmd/sessions/set/set.manual.go +++ b/pkg/cmd/sessions/set/set.manual.go @@ -221,7 +221,7 @@ func (n *CmdSet) RunE(cmd *cobra.Command, args []string) error { Username: handler.C8Yclient.Username, } - outputFormat := cfg.GetOutputFormatWithDefault(config.OutputUnknown).String() + outputFormat := cfg.GetOutputFormatWithDefault(cmd, config.OutputUnknown).String() // Write session details to stderr (for humans) if outputFormat != config.OutputJSON.String() { diff --git a/pkg/config/cliConfiguration.go b/pkg/config/cliConfiguration.go index 03f47b19b..36c1c5d93 100644 --- a/pkg/config/cliConfiguration.go +++ b/pkg/config/cliConfiguration.go @@ -1449,12 +1449,15 @@ func (c *Config) GetOutputFormat() OutputFormat { } // GetOutputFormat Get output format type, i.e. json, csv, table etc. -func (c *Config) GetOutputFormatWithDefault(fallback OutputFormat) OutputFormat { - if c.RawOutput() { - return OutputJSON +func (c *Config) GetOutputFormatWithDefault(cmd *cobra.Command, fallback OutputFormat) OutputFormat { + if !cmd.Flags().Changed("output") { + return fallback } - format := c.viper.GetString(SettingsOutputFormat) - return fallback.FromString(format) + value, err := cmd.Flags().GetString("output") + if err != nil { + return fallback + } + return fallback.FromString(value) } // IsCSVOutput check if csv output is enabled