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

Catch "diff" command panics within external library deps. and exit gracefully #80

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
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
14 changes: 8 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<filename>", "--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"]
},
]
Expand Down
42 changes: 31 additions & 11 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -157,29 +161,28 @@ 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:
var aJson map[string]interface{}
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
}

Expand All @@ -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)
Expand All @@ -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
}
Loading