Skip to content

Commit 0b3a4fc

Browse files
Refactor Semgrep analysis function to return error (#76)
Updated the runSemgrepAnalysis function to return an error instead of logging fatal errors directly. This change allows for better error handling in the calling function. Additionally, cleaned up the formatting of the default versions in the config file template for improved readability.
1 parent 83238e7 commit 0b3a4fc

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

cmd/analyze.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,13 @@ func runDartAnalyzer(workDirectory string, pathsToCheck []string, outputFile str
222222
return tools.RunDartAnalyzer(workDirectory, dartanalyzer.InstallDir, dartanalyzer.Binaries["dart"], pathsToCheck, outputFile, outputFormat)
223223
}
224224

225-
func runSemgrepAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) {
225+
func runSemgrepAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
226226
semgrep := config.Config.Tools()["semgrep"]
227227
if semgrep == nil {
228228
log.Fatal("Semgrep tool configuration not found")
229229
}
230230

231-
err := tools.RunSemgrep(workDirectory, semgrep, pathsToCheck, outputFile, outputFormat)
232-
if err != nil {
233-
log.Fatalf("Failed to run Semgrep analysis: %v", err)
234-
}
231+
return tools.RunSemgrep(workDirectory, semgrep, pathsToCheck, outputFile, outputFormat)
235232
}
236233

237234
var analyzeCmd = &cobra.Command{
@@ -323,7 +320,7 @@ func runTool(workDirectory string, toolName string, args []string, outputFile st
323320
case "pmd":
324321
return runPmdAnalysis(workDirectory, args, outputFile, outputFormat)
325322
case "pylint":
326-
return runPylintAnalysis(workDirectory, args, outputFile, outputFormat)
323+
return runPylintAnalysis(workDirectory, args, outputFile, outputFormat)
327324
case "semgrep":
328325
return runSemgrepAnalysis(workDirectory, args, outputFile, outputFormat)
329326
case "dartanalyzer":

cmd/init.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ func configFileTemplate(tools []tools.Tool) string {
141141
needsDart := false
142142
// Default versions
143143
defaultVersions := map[string]string{
144-
ESLint: "9.3.0",
145-
Trivy: "0.59.1",
146-
PyLint: "3.3.6",
147-
PMD: "6.55.0",
144+
ESLint: "9.3.0",
145+
Trivy: "0.59.1",
146+
PyLint: "3.3.6",
147+
PMD: "6.55.0",
148148
DartAnalyzer: "3.7.2",
149-
Semgrep: "1.78.0",
149+
Semgrep: "1.78.0",
150150
}
151151

152152
// Build map of enabled tools with their versions
@@ -201,7 +201,7 @@ func configFileTemplate(tools []tools.Tool) string {
201201
PyLint: "pylint",
202202
PMD: "pmd",
203203
DartAnalyzer: "dartanalyzer",
204-
Semgrep: "semgrep",
204+
Semgrep: "semgrep",
205205
}
206206

207207
for uuid, name := range uuidToName {
@@ -393,6 +393,20 @@ func createToolFileConfigurations(tool tools.Tool, patternConfiguration []domain
393393
}
394394
}
395395
fmt.Println("Pylint configuration created based on Codacy settings")
396+
case DartAnalyzer:
397+
if len(patternConfiguration) > 0 {
398+
err := createDartAnalyzerConfigFile(patternConfiguration, toolsConfigDir)
399+
if err != nil {
400+
return fmt.Errorf("failed to create Dart Analyzer config: %v", err)
401+
}
402+
}
403+
case Semgrep:
404+
if len(patternConfiguration) > 0 {
405+
err := createSemgrepConfigFile(patternConfiguration, toolsConfigDir)
406+
if err != nil {
407+
return fmt.Errorf("failed to create Semgrep config: %v", err)
408+
}
409+
}
396410
}
397411
return nil
398412
}
@@ -504,5 +518,5 @@ const (
504518
PMD string = "9ed24812-b6ee-4a58-9004-0ed183c45b8f"
505519
PyLint string = "31677b6d-4ae0-4f56-8041-606a8d7a8e61"
506520
DartAnalyzer string = "d203d615-6cf1-41f9-be5f-e2f660f7850f"
507-
Semgrep string = "6792c561-236d-41b7-ba5e-9d6bee0d548b"
521+
Semgrep string = "6792c561-236d-41b7-ba5e-9d6bee0d548b"
508522
)

0 commit comments

Comments
 (0)