Skip to content

Commit fd9c3c7

Browse files
authored
Simplify cmd/bundle/{summary,validate}.go (#3182)
## Why The common function added in #3123 make it harder to understand what's going on and I removed it in #3175 Sending this as separate PR to make #3175 easier to review.
1 parent 3e00170 commit fd9c3c7

File tree

2 files changed

+48
-51
lines changed

2 files changed

+48
-51
lines changed

cmd/bundle/summary.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"github.com/databricks/cli/bundle/config/mutator"
1010
"github.com/databricks/cli/bundle/deploy/terraform"
1111
"github.com/databricks/cli/bundle/phases"
12+
"github.com/databricks/cli/bundle/render"
1213
"github.com/databricks/cli/bundle/statemgmt"
1314
"github.com/databricks/cli/cmd/bundle/utils"
1415
"github.com/databricks/cli/cmd/root"
1516
"github.com/databricks/cli/libs/diag"
17+
"github.com/databricks/cli/libs/flags"
1618
"github.com/spf13/cobra"
1719
)
1820

@@ -31,7 +33,37 @@ func newSummaryCommand() *cobra.Command {
3133

3234
cmd.RunE = func(cmd *cobra.Command, args []string) error {
3335
b, diags := prepareBundleForSummary(cmd, forcePull, includeLocations)
34-
return renderBundle(cmd, b, diags, true)
36+
var err error
37+
38+
if root.OutputType(cmd) == flags.OutputText {
39+
err = render.RenderDiagnostics(cmd.OutOrStdout(), b, diags, render.RenderOptions{RenderSummaryTable: false})
40+
} else {
41+
err = render.RenderDiagnostics(cmd.OutOrStderr(), b, diags, render.RenderOptions{RenderSummaryTable: false})
42+
}
43+
if err != nil {
44+
return err
45+
}
46+
47+
if b != nil {
48+
if root.OutputType(cmd) == flags.OutputText {
49+
err = render.RenderSummary(cmd.Context(), cmd.OutOrStdout(), b)
50+
if err != nil {
51+
return err
52+
}
53+
}
54+
if root.OutputType(cmd) == flags.OutputJSON {
55+
err = renderJsonOutput(cmd, b)
56+
if err != nil {
57+
return err
58+
}
59+
}
60+
}
61+
62+
if diags.HasError() {
63+
return root.ErrAlreadyPrinted
64+
}
65+
66+
return nil
3567
}
3668

3769
return cmd

cmd/bundle/validate.go

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package bundle
33
import (
44
"encoding/json"
55
"errors"
6-
"fmt"
76

87
"github.com/databricks/cli/bundle"
98
"github.com/databricks/cli/bundle/config/mutator"
@@ -12,7 +11,6 @@ import (
1211
"github.com/databricks/cli/bundle/render"
1312
"github.com/databricks/cli/cmd/bundle/utils"
1413
"github.com/databricks/cli/cmd/root"
15-
"github.com/databricks/cli/libs/diag"
1614
"github.com/databricks/cli/libs/flags"
1715
"github.com/spf13/cobra"
1816
)
@@ -67,60 +65,27 @@ func newValidateCommand() *cobra.Command {
6765
diags = diags.Extend(bundle.Apply(ctx, b, mutator.PopulateLocations()))
6866
}
6967

70-
return renderBundle(cmd, b, diags, false)
71-
}
72-
73-
return cmd
74-
}
75-
76-
// This function is used to render results both for "bundle validate" and "bundle summary".
77-
// In JSON mode, there is no difference in rendering between these two (but there is a difference in how we prepare the bundle).
78-
// In non-JSON mode both "bundle validate" and "bundle summary" will print diagnostics to stderr but "bundle validate"
79-
// will also print "summary" message via RenderSummaryTable option.
80-
func renderBundle(cmd *cobra.Command, b *bundle.Bundle, diags diag.Diagnostics, withBundleSummary bool) error {
81-
ctx := cmd.Context()
82-
switch root.OutputType(cmd) {
83-
case flags.OutputText:
84-
// Confusingly RenderSummaryTable relates to "Validation OK" and related messages, it has nothing
85-
// to do with "bundle summary" command and we don't want to show it in bundle summary command.
86-
renderOpts := render.RenderOptions{RenderSummaryTable: !withBundleSummary}
87-
err1 := render.RenderDiagnostics(cmd.OutOrStdout(), b, diags, renderOpts)
88-
if b != nil && withBundleSummary {
89-
// Now RenderSummary actually related to "bundle summary"
90-
err2 := render.RenderSummary(ctx, cmd.OutOrStdout(), b)
91-
if err2 != nil {
92-
return err2
68+
if root.OutputType(cmd) == flags.OutputText {
69+
err := render.RenderDiagnostics(cmd.OutOrStdout(), b, diags, render.RenderOptions{RenderSummaryTable: true})
70+
if err != nil {
71+
return err
9372
}
9473
}
95-
96-
if err1 != nil {
97-
return err1
98-
}
99-
100-
if diags.HasError() {
101-
return root.ErrAlreadyPrinted
102-
}
103-
104-
return nil
105-
case flags.OutputJSON:
106-
renderOpts := render.RenderOptions{RenderSummaryTable: false}
107-
err1 := render.RenderDiagnostics(cmd.ErrOrStderr(), b, diags, renderOpts)
108-
err2 := renderJsonOutput(cmd, b)
109-
110-
if err2 != nil {
111-
return err2
112-
}
113-
114-
if err1 != nil {
115-
return err1
74+
if root.OutputType(cmd) == flags.OutputJSON {
75+
err := render.RenderDiagnostics(cmd.ErrOrStderr(), b, diags, render.RenderOptions{RenderSummaryTable: false})
76+
if err != nil {
77+
return err
78+
}
79+
err = renderJsonOutput(cmd, b)
80+
if err != nil {
81+
return err
82+
}
11683
}
117-
11884
if diags.HasError() {
11985
return root.ErrAlreadyPrinted
12086
}
121-
12287
return nil
123-
default:
124-
return fmt.Errorf("unknown output type %s", root.OutputType(cmd))
12588
}
89+
90+
return cmd
12691
}

0 commit comments

Comments
 (0)