Skip to content

Commit 0f94ba9

Browse files
authored
Add CSV output for validation errors and further streamline formatting code path (#43)
* Assure txt format still prints schema errors with quiet flag Signed-off-by: Matt Rutkowski <[email protected]> * rename error type normalization function to reduce output size Signed-off-by: Matt Rutkowski <[email protected]> * rename error type normalization function to reduce output size Signed-off-by: Matt Rutkowski <[email protected]> * rename error type normalization function to reduce output size Signed-off-by: Matt Rutkowski <[email protected]> * Assure all validate tests output valid JSON where applicable Signed-off-by: Matt Rutkowski <[email protected]> * Support csv formatted output for validation errors Signed-off-by: Matt Rutkowski <[email protected]> * Fix G104 linter complaint Signed-off-by: Matt Rutkowski <[email protected]> * Fix G104 linter complaint Signed-off-by: Matt Rutkowski <[email protected]> --------- Signed-off-by: Matt Rutkowski <[email protected]>
1 parent 27065e5 commit 0f94ba9

File tree

5 files changed

+187
-68
lines changed

5 files changed

+187
-68
lines changed

.vscode/settings.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"gojsonschema",
4141
"gomod",
4242
"GTPL",
43+
"hashstructure",
4344
"HBOM",
4445
"hokaccha",
4546
"HSQLDB",

cmd/license_list.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func DisplayLicenseListJson(output io.Writer) {
281281
}
282282
json, _ := log.FormatInterfaceAsJson(lc)
283283

284-
// Note: JSON data files MUST ends in a newline s as this is a POSIX standard
284+
// Note: JSON data files MUST ends in a newline as this is a POSIX standard
285285
fmt.Fprintf(output, "%s\n", json)
286286
}
287287

@@ -330,7 +330,7 @@ func DisplayLicenseListCSV(output io.Writer) (err error) {
330330
lc.License.Text.Content)
331331

332332
if errWrite := w.Write(currentRow); errWrite != nil {
333-
return getLogger().Errorf("error writing to output (%v): %s", currentRow, err)
333+
return getLogger().Errorf("error writing to output (%v): %s", currentRow, errWrite)
334334
}
335335
}
336336
}

cmd/validate.go

+19-15
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const (
5757
)
5858

5959
var VALIDATE_SUPPORTED_ERROR_FORMATS = MSG_VALIDATE_FLAG_ERR_FORMAT +
60-
strings.Join([]string{FORMAT_TEXT, FORMAT_JSON}, ", ") + " (default: txt)"
60+
strings.Join([]string{FORMAT_TEXT, FORMAT_JSON, FORMAT_CSV}, ", ") + " (default: txt)"
6161

6262
// limits
6363
const (
@@ -150,8 +150,9 @@ func validateCmdImpl(cmd *cobra.Command, args []string) error {
150150
return nil
151151
}
152152

153-
// Normalize error/normalizeValidationErrorTypes from the Validate() function
154-
func normalizeValidationErrorTypes(document *schema.Sbom, valid bool, err error) {
153+
// Normalize ErrorTypes from the Validate() function
154+
// Note: this function name should not be changed
155+
func validationError(document *schema.Sbom, valid bool, err error) {
155156

156157
// Consistently display errors before exiting
157158
if err != nil {
@@ -185,7 +186,8 @@ func Validate(output io.Writer, persistentFlags utils.PersistentCommandFlags, va
185186
// use function closure to assure consistent error output based upon error type
186187
defer func() {
187188
if err != nil {
188-
normalizeValidationErrorTypes(document, valid, err)
189+
// normalize the error output to console
190+
validationError(document, valid, err)
189191
}
190192
}()
191193

@@ -280,7 +282,7 @@ func Validate(output io.Writer, persistentFlags utils.PersistentCommandFlags, va
280282
getLogger().Infof("SBOM valid against JSON schema: `%t`", result.Valid())
281283
valid = result.Valid()
282284

283-
// Catch general errors from the validation module itself and pass them on'
285+
// Catch general errors from the validation package/library itself and display them
284286
if errValidate != nil {
285287
// we force result to INVALID as any errors from the library means
286288
// we could NOT actually confirm the input documents validity
@@ -297,19 +299,21 @@ func Validate(output io.Writer, persistentFlags utils.PersistentCommandFlags, va
297299
schemaErrors)
298300

299301
// TODO: de-duplicate errors (e.g., array item not "unique"...)
300-
var formattedErrors string
301-
switch persistentFlags.OutputFormat {
302+
format := persistentFlags.OutputFormat
303+
switch format {
302304
case FORMAT_JSON:
303-
// Note: JSON data files MUST ends in a newline s as this is a POSIX standard
304-
formattedErrors = FormatSchemaErrors(schemaErrors, validateFlags, FORMAT_JSON)
305-
// getLogger().Debugf("%s", formattedErrors)
306-
fmt.Fprintf(output, "%s", formattedErrors)
307-
case FORMAT_TEXT:
308305
fallthrough
306+
case FORMAT_CSV:
307+
fallthrough
308+
case FORMAT_TEXT:
309+
// Note: we no longer add the formatted errors to the actual error "detail" field;
310+
// since BOMs can have large numbers of errors. The new method is to allow
311+
// the user to control the error result output (e.g., file, detail, etc.) via flags
312+
FormatSchemaErrors(output, schemaErrors, validateFlags, format)
309313
default:
310-
// Format error results and append to InvalidSBOMError error "details"
311-
formattedErrors = FormatSchemaErrors(schemaErrors, validateFlags, FORMAT_TEXT)
312-
errInvalid.Details = formattedErrors
314+
// Notify caller that we are defaulting to "txt" format
315+
getLogger().Warningf(MSG_WARN_INVALID_FORMAT, format, FORMAT_TEXT)
316+
FormatSchemaErrors(output, schemaErrors, validateFlags, FORMAT_TEXT)
313317
}
314318

315319
return INVALID, document, schemaErrors, errInvalid

0 commit comments

Comments
 (0)