diff --git a/.vscode/launch.json b/.vscode/launch.json index 6ffd93d1..47e07df0 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,31 +6,33 @@ "configurations": [ { - "name": "DebugServer", + "showGlobalVariables": true, + "name": "Debug: query: SELECT * FROM metadata.component", "type": "go", "request": "launch", "mode": "debug", - "program": "${workspaceFolder}", + "program": "main.go", // "program": "${file}", + "args": ["query", "-i", "test/cyclonedx/cdx-1-4-mature-example-1.json", "--select", "*", "--from", "metadata.component"], "dlvFlags": ["--check-go-version=false"] }, { "showGlobalVariables": true, - "name": "Query: SELECT * FROM metadata.component", + "name": "Debug: license list", "type": "go", "request": "launch", "mode": "debug", "program": "main.go", // "program": "${file}", - "args": ["query", "-i", "test/cyclonedx/cdx-1-4-mature-example-1.json", "--select", "*", "--from", "metadata.component"], + "args": ["license", "list", "-i", "", "--format", "json"], "dlvFlags": ["--check-go-version=false"] }, { "showGlobalVariables": true, - "name": "Debug: license list --format csv", + "name": "Debug: diff", "type": "go", "request": "launch", "mode": "debug", "program": "main.go", // "program": "${file}", - "args": ["license", "list", "-i", "/Users/Matt_1/Downloads/acdx.json", "--format", "csv"], + "args": ["diff", "-i", "test/cyclonedx/cdx-1-4-mature-example-1.json", "--input-revision", "test/diff/cdx-1-4-mature-example-1-delta.json"], "dlvFlags": ["--check-go-version=false"] }, ] diff --git a/cmd/diff.go b/cmd/diff.go index dd782906..b460eda2 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -115,7 +115,11 @@ func diffCmdImpl(cmd *cobra.Command, args []string) (err error) { }() err = Diff(utils.GlobalFlags.PersistentFlags, utils.GlobalFlags.DiffFlags) - + // Note: we turn diff library panics into errors that should change exit status code + if err != nil { + getLogger().Errorf("diff failed: differences between files perhaps too large.") + os.Exit(ERROR_APPLICATION) + } return } @@ -148,7 +152,7 @@ func Diff(persistentFlags utils.PersistentCommandFlags, flags utils.DiffCommandF bBaseData, errReadBase := os.ReadFile(inputFilename) if errReadBase != nil { getLogger().Debugf("%v", bBaseData[:255]) - err = getLogger().Errorf("Failed to ReadFile '%s': %s\n", inputFilename, err.Error()) + err = getLogger().Errorf("Failed to ReadFile '%s': %s", inputFilename, err.Error()) return } @@ -157,21 +161,20 @@ func Diff(persistentFlags utils.PersistentCommandFlags, flags utils.DiffCommandF bRevisedData, errReadDelta := os.ReadFile(revisedFilename) if errReadDelta != nil { getLogger().Debugf("%v", bRevisedData[:255]) - err = getLogger().Errorf("Failed to ReadFile '%s': %s\n", inputFilename, err.Error()) + err = getLogger().Errorf("Failed to ReadFile '%s': %s", inputFilename, err.Error()) return } // Compare the base with the revision - differ := diff.New() getLogger().Infof("Comparing files: `%s` (base) to `%s` (revised) ...", inputFilename, revisedFilename) - d, err := differ.Compare(bBaseData, bRevisedData) - if err != nil { - err = getLogger().Errorf("Failed to Compare data: %s\n", err.Error()) + diffResults, errCompare := compareBinaryData(bBaseData, bRevisedData) + if errCompare != nil { + return errCompare } // Output the result var diffString string - if d.Modified() { + if diffResults.Modified() { getLogger().Infof("Outputting listing (`%s` format)...", format) switch outputFormat { case FORMAT_TEXT: @@ -179,7 +182,7 @@ func Diff(persistentFlags utils.PersistentCommandFlags, flags utils.DiffCommandF err = json.Unmarshal(bBaseData, &aJson) if err != nil { - err = getLogger().Errorf("json.Unmarshal() failed '%s': %s\n", inputFilename, err.Error()) + err = getLogger().Errorf("json.Unmarshal() failed '%s': %s", inputFilename, err.Error()) return } @@ -188,10 +191,10 @@ func Diff(persistentFlags utils.PersistentCommandFlags, flags utils.DiffCommandF } config.Coloring = deltaColorize formatter := formatter.NewAsciiFormatter(aJson, config) - diffString, err = formatter.Format(d) + diffString, err = formatter.Format(diffResults) case FORMAT_JSON: formatter := formatter.NewDeltaFormatter() - diffString, err = formatter.Format(d) + diffString, err = formatter.Format(diffResults) // Note: JSON data files MUST ends in a newline as this is a POSIX standard default: // Default to Text output for anything else (set as flag default) @@ -207,3 +210,20 @@ func Diff(persistentFlags utils.PersistentCommandFlags, flags utils.DiffCommandF return } + +func compareBinaryData(bBaseData []byte, bRevisedData []byte) (diffResults diff.Diff, err error) { + defer func() { + if recoveredPanic := recover(); recoveredPanic != nil { + fmt.Println("panic occurred:", recoveredPanic) + err = getLogger().Errorf("panic occurred: %v", recoveredPanic) + return + } + }() + + differ := diff.New() + diffResults, err = differ.Compare(bBaseData, bRevisedData) + if err != nil { + err = getLogger().Errorf("differ.Compare() failed: %s", err.Error()) + } + return +}