From 784f006da5a1d9d82e96d53330cefd1bf5483c39 Mon Sep 17 00:00:00 2001 From: Keepers Date: Thu, 1 Sep 2022 16:06:35 -0600 Subject: [PATCH] don't split json output by type (#725) ## Description Prevents output printing of detail entries from splitting into multiple json arrays when the entries contains multiple item type.s ## Type of change Please check the type of change your PR introduces: - [x] :bug: Bugfix ## Issue(s) #501 ## Test Plan - [x] :muscle: Manual - [ ] :zap: Unit test - [ ] :green_heart: E2E --- src/cli/print/print.go | 5 +++++ src/pkg/backup/details/details.go | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/cli/print/print.go b/src/cli/print/print.go index d50315170..6d82f204c 100644 --- a/src/cli/print/print.go +++ b/src/cli/print/print.go @@ -46,6 +46,11 @@ func AddOutputFlag(parent *cobra.Command) { cobra.CheckErr(fs.MarkHidden("json-debug")) } +// JSONFormat returns true if the printer plans to output as json. +func JSONFormat() bool { + return outputAsJSON || outputAsJSONDebug +} + // --------------------------------------------------------------------------------------------------------- // Helper funcs // --------------------------------------------------------------------------------------------------------- diff --git a/src/pkg/backup/details/details.go b/src/pkg/backup/details/details.go index 9cc2fe927..a19aa50a2 100644 --- a/src/pkg/backup/details/details.go +++ b/src/pkg/backup/details/details.go @@ -24,6 +24,14 @@ type DetailsModel struct { // Print writes the DetailModel Entries to StdOut, in the format // requested by the caller. func (dm DetailsModel) PrintEntries(ctx context.Context) { + if print.JSONFormat() { + printJSON(ctx, dm) + } else { + printTable(ctx, dm) + } +} + +func printTable(ctx context.Context, dm DetailsModel) { perType := map[itemType][]print.Printable{} for _, de := range dm.Entries { @@ -42,6 +50,16 @@ func (dm DetailsModel) PrintEntries(ctx context.Context) { } } +func printJSON(ctx context.Context, dm DetailsModel) { + ents := []print.Printable{} + + for _, ent := range dm.Entries { + ents = append(ents, print.Printable(ent)) + } + + print.All(ctx, ents...) +} + // Paths returns the list of Paths extracted from the Entries slice. func (dm DetailsModel) Paths() []string { ents := dm.Entries