Skip to content

Commit

Permalink
Update yaml output to be consistent with json output (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakemalachowski authored Dec 4, 2024
1 parent 4ba0860 commit 1d51720
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pkg/command/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 1d51720

Please sign in to comment.