generated from ossf/project-template
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add data validator, validate subcommand (#188)
* Add validate subcommand Signed-off-by: Adolfo García Veytia (Puerco) <adolfo.garcia@uservers.net> * Prevent rendering with invalid data data Signed-off-by: Adolfo García Veytia (Puerco) <adolfo.garcia@uservers.net> --------- Signed-off-by: Adolfo García Veytia (Puerco) <adolfo.garcia@uservers.net>
- Loading branch information
Showing
5 changed files
with
122 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// SPDX-FileCopyrightText: Copyright 2025 The OSPS Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/ossf/security-baseline/pkg/baseline" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type validateOptions struct { | ||
baselinePath string | ||
} | ||
|
||
// Validate the options in context with arguments | ||
func (o *validateOptions) Validate() error { | ||
errs := []error{} | ||
|
||
if o.baselinePath == "" { | ||
errs = append(errs, errors.New("baseline data path not specified")) | ||
} | ||
return errors.Join(errs...) | ||
} | ||
|
||
func (o *validateOptions) AddFlags(cmd *cobra.Command) { | ||
cmd.PersistentFlags().StringVarP( | ||
&o.baselinePath, "baseline", "b", "", "path to directory containing the baseline YAML definitions", | ||
) | ||
|
||
} | ||
|
||
// addValidate adds the compile subcommand to the parent command | ||
func addValidate(parentCmd *cobra.Command) { | ||
opts := validateOptions{} | ||
validateCmd := &cobra.Command{ | ||
Use: "validate", | ||
Short: "Validate the baseline data files", | ||
SilenceUsage: false, | ||
SilenceErrors: true, | ||
PreRunE: func(_ *cobra.Command, args []string) error { | ||
if opts.baselinePath != "" && len(args) > 0 && opts.baselinePath != args[0] { | ||
return fmt.Errorf("baseline data path specified twice") | ||
} | ||
|
||
if len(args) > 0 { | ||
opts.baselinePath = args[0] | ||
} | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if err := opts.Validate(); err != nil { | ||
return err | ||
} | ||
|
||
cmd.SilenceUsage = true | ||
|
||
// Parse the data files | ||
loader := baseline.NewLoader() | ||
loader.DataPath = opts.baselinePath | ||
|
||
bline, err := loader.Load() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Generate the rendered version | ||
validator := baseline.NewValidator() | ||
|
||
if err = validator.Check(bline); err != nil { | ||
fmt.Fprint(os.Stderr, "\n❌ Error validating the baseline data:\n") | ||
return err | ||
} | ||
fmt.Fprint(os.Stderr, "\n✅ Baseline YAML data OK\n\n") | ||
return nil | ||
}, | ||
} | ||
opts.AddFlags(validateCmd) | ||
parentCmd.AddCommand(validateCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters