Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lint): lint now waits until exiting the loop to write to file, handles multiple validation results #504

Merged
merged 4 commits into from
Jun 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions src/cmd/tools/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package tools

import (
"encoding/json"
"fmt"
"strings"

"github.com/defenseunicorns/go-oscal/src/pkg/validation"
"github.com/defenseunicorns/lula/src/config"
Expand Down Expand Up @@ -31,6 +33,7 @@ func init() {
Long: "Validate OSCAL documents are properly configured against the OSCAL schema",
Example: lintHelp,
Run: func(cmd *cobra.Command, args []string) {
var validationResults []validation.ValidationResult
if len(opts.InputFiles) == 0 {
message.Fatalf(nil, "No input files specified")
}
Expand All @@ -49,28 +52,48 @@ func init() {
message.Warn(warning)
}

if opts.ResultFile != "" {
validation.WriteValidationResult(validationResp.Result, opts.ResultFile)
} else {
// append the validation result to the results array
validationResults = append(validationResults, validationResp.Result)

// If result file is not specified, print the validation result
if opts.ResultFile == "" {
jsonBytes, err := json.MarshalIndent(validationResp.Result, "", " ")
if err != nil {
message.Fatalf(err, "Failed to marshal validation result")
}
message.Infof("Validation result for %s: %s", inputFile, string(jsonBytes))
}

if validationResp.JsonSchemaError != nil {
message.Fatalf(err, "Failed to lint %s", inputFile)
}

// New conditional for logging success or failed linting
if validationResp.Result.Valid {
message.Infof("Successfully validated %s is valid OSCAL version %s %s\n", inputFile, validationResp.Validator.GetSchemaVersion(), validationResp.Validator.GetModelType())
spinner.Success()
} else {
message.Fatalf(nil, "Failed linting for %s", inputFile)
message.WarnErrf(nil, "Failed to lint %s", inputFile)
spinner.Stop()
}
}

// If result file is specified, write the validation results to the file
if opts.ResultFile != "" {
// If there is only one validation result, write it to the file
if len(validationResults) == 1 {
validation.WriteValidationResult(validationResults[0], opts.ResultFile)
} else {
// If there are multiple validation results, write them to the file
validation.WriteValidationResults(validationResults, opts.ResultFile)
}
}

// If there is at least one validation result that is not valid, exit with a fatal error
failedFiles := []string{}
for _, result := range validationResults {
if !result.Valid {
failedFiles = append(failedFiles, result.Metadata.DocumentPath)
}
}
if len(failedFiles) > 0 {
message.Fatal(nil, fmt.Sprintf("The following files failed linting: %s", strings.Join(failedFiles, ", ")))
}
},
}

Expand All @@ -79,4 +102,3 @@ func init() {
lintCmd.Flags().StringSliceVarP(&opts.InputFiles, "input-files", "f", []string{}, "the paths to oscal json schema files (comma-separated)")
lintCmd.Flags().StringVarP(&opts.ResultFile, "result-file", "r", "", "the path to write the validation result")
}