Skip to content

Commit 9f6cd7c

Browse files
authored
Make cmd/bundle/validate.go easier to follow (#3196)
Use the same structure as cmd/bundle/summary.go to have one function for setting up the bundle.
1 parent f20ddd3 commit 9f6cd7c

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

cmd/bundle/validate.go

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,23 @@ func newValidateCommand() *cobra.Command {
4545
ctx := logdiag.InitContext(cmd.Context())
4646
cmd.SetContext(ctx)
4747

48-
b := utils.ConfigureBundleWithVariables(cmd)
48+
b := prepareBundleForValidate(cmd, includeLocations)
4949

5050
if b == nil {
51-
if !logdiag.HasError(ctx) {
51+
if logdiag.HasError(ctx) {
52+
return root.ErrAlreadyPrinted
53+
} else {
5254
return errors.New("invariant failed: returned bundle is nil")
5355
}
5456
}
5557

56-
if !logdiag.HasError(ctx) {
57-
phases.Initialize(ctx, b)
58-
}
59-
60-
if !logdiag.HasError(ctx) {
61-
validate.Validate(ctx, b)
62-
}
63-
64-
// Include location information in the output if the flag is set.
65-
if includeLocations {
66-
bundle.ApplyContext(ctx, b, mutator.PopulateLocations())
67-
}
68-
6958
if root.OutputType(cmd) == flags.OutputText {
7059
err := render.RenderDiagnosticsSummary(ctx, cmd.OutOrStdout(), b)
7160
if err != nil {
7261
return err
7362
}
7463
}
64+
7565
if root.OutputType(cmd) == flags.OutputJSON {
7666
err := renderJsonOutput(cmd, b)
7767
if err != nil {
@@ -88,3 +78,31 @@ func newValidateCommand() *cobra.Command {
8878

8979
return cmd
9080
}
81+
82+
func prepareBundleForValidate(cmd *cobra.Command, includeLocations bool) *bundle.Bundle {
83+
b := utils.ConfigureBundleWithVariables(cmd)
84+
ctx := cmd.Context()
85+
86+
if b == nil || logdiag.HasError(ctx) {
87+
return b
88+
}
89+
90+
phases.Initialize(ctx, b)
91+
92+
if logdiag.HasError(ctx) {
93+
return b
94+
}
95+
96+
validate.Validate(ctx, b)
97+
98+
if logdiag.HasError(ctx) {
99+
return b
100+
}
101+
102+
// Include location information in the output if the flag is set.
103+
if includeLocations {
104+
bundle.ApplyContext(ctx, b, mutator.PopulateLocations())
105+
}
106+
107+
return b
108+
}

0 commit comments

Comments
 (0)