From 1d51720bc2ce9349c12175a485ddad9331ed75c1 Mon Sep 17 00:00:00 2001 From: Jake Malachowski <5766239+jakemalachowski@users.noreply.github.com> Date: Wed, 4 Dec 2024 09:23:50 -0600 Subject: [PATCH] Update yaml output to be consistent with json output (#164) --- pkg/command/wrapper.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/command/wrapper.go b/pkg/command/wrapper.go index 8938b4e..780931e 100644 --- a/pkg/command/wrapper.go +++ b/pkg/command/wrapper.go @@ -91,7 +91,20 @@ func PrintData[T any](cmd *cobra.Command, data T, formatText FormatTextFunc[T]) case JSON: return true, printJSON(cmd, data) case YAML: - yamlStr, err := yaml.Marshal(data) + // Convert to JSON before converting to YAML to remove the top-level key of the containing struct and + // null values that have omit_empty json tags. This is for consistency between JSON and YAML output. + jsonStr, err := json.Marshal(data) + if err != nil { + return true, err + } + + var yamlData interface{} + err = json.Unmarshal(jsonStr, &yamlData) + if err != nil { + return true, err + } + + yamlStr, err := yaml.Marshal(yamlData) if err != nil { return true, err }