Skip to content

Commit 0149962

Browse files
committed
Prevent rendering with invalid data data
Signed-off-by: Adolfo García Veytia (Puerco) <adolfo.garcia@uservers.net>
1 parent 307c3a8 commit 0149962

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

cmd/internal/cmd/compile.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type compileOptions struct {
1616
outPath string
1717
baselinePath string
1818
templatePath string
19+
validate bool
1920
}
2021

2122
// Validate the options in context with arguments
@@ -44,6 +45,10 @@ func (o *compileOptions) AddFlags(cmd *cobra.Command) {
4445
cmd.PersistentFlags().StringVarP(
4546
&o.templatePath, "template", "t", "template.md", "path to the markdown template file",
4647
)
48+
49+
cmd.PersistentFlags().BoolVarP(
50+
&o.validate, "validate", "v", true, "validate data inegrity before rendering",
51+
)
4752
}
4853

4954
// addCompile adds the compile subcommand to the parent command
@@ -71,6 +76,7 @@ func addCompile(parentCmd *cobra.Command) {
7176

7277
cmd.SilenceUsage = true
7378

79+
// Load the baseline data
7480
loader := baseline.NewLoader()
7581
loader.DataPath = opts.baselinePath
7682

@@ -79,6 +85,20 @@ func addCompile(parentCmd *cobra.Command) {
7985
return err
8086
}
8187

88+
// Validate the data
89+
validator := baseline.NewValidator()
90+
err = validator.Check(bline)
91+
if err != nil {
92+
if opts.validate {
93+
fmt.Fprint(os.Stderr, "\n❌ Error validating the baseline data:\n")
94+
return err
95+
}
96+
97+
// if the validation flag is off, we still validate but only warn
98+
// the user about it
99+
fmt.Fprint(os.Stderr, "\n⚠️ Error validating the baseline data (still rendering)")
100+
}
101+
82102
// Generate the rendered version
83103
gen := baseline.NewGenerator()
84104
gen.TemplatePath = opts.templatePath
@@ -87,7 +107,7 @@ func addCompile(parentCmd *cobra.Command) {
87107
return fmt.Errorf("writing mardown render: %w", err)
88108
}
89109

90-
fmt.Fprintf(os.Stderr, "\nBaseline rendered to %s:\n\nCategories:\n", opts.outPath)
110+
fmt.Fprintf(os.Stderr, "\n✅ Baseline rendered to %s:\n\nCategories:\n", opts.outPath)
91111
for c := range bline.Categories {
92112
fmt.Fprintf(os.Stderr, " OSPS-%s: %d criteria\n", c, len(bline.Categories[c].Criteria))
93113
}

0 commit comments

Comments
 (0)