Skip to content

Commit a934839

Browse files
committed
Add enigma-cli to cli v2
1 parent fc9ee88 commit a934839

File tree

6 files changed

+146
-1
lines changed

6 files changed

+146
-1
lines changed

.codacy/codacy.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ tools:
99
1010
1111
12-
12+
13+

cmd/analyze.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,15 @@ func runLizardAnalysis(workDirectory string, pathsToCheck []string, outputFile s
411411
return lizard.RunLizard(workDirectory, lizardBinary, pathsToCheck, outputFile, outputFormat, patterns)
412412
}
413413

414+
func runEnigmaAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
415+
enigma := config.Config.Tools()["codacy-enigma-cli"]
416+
if enigma == nil {
417+
log.Fatal("Enigma tool configuration not found")
418+
}
419+
420+
return tools.RunEnigma(workDirectory, enigma.InstallDir, enigma.Binaries["codacy-enigma-cli"], pathsToCheck, outputFile, outputFormat)
421+
}
422+
414423
var analyzeCmd = &cobra.Command{
415424
Use: "analyze",
416425
Short: "Runs all configured linters.",
@@ -516,6 +525,8 @@ func runTool(workDirectory string, toolName string, args []string, outputFile st
516525
return runDartAnalyzer(workDirectory, args, outputFile, outputFormat)
517526
case "lizard":
518527
return runLizardAnalysis(workDirectory, args, outputFile, outputFormat)
528+
case "codacy-enigma-cli":
529+
return runEnigmaAnalysis(workDirectory, args, outputFile, outputFormat)
519530
default:
520531
return fmt.Errorf("unsupported tool: %s", toolName)
521532
}

plugins/tool-utils_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ func TestGetSupportedTools(t *testing.T) {
169169
"dartanalyzer",
170170
"semgrep",
171171
"lizard",
172+
"codacy-enigma-cli",
172173
},
173174
expectedError: false,
174175
},
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: codacy-enigma-cli
2+
description: Enigma CLI
3+
download:
4+
url_template: "https://github.com/codacy/codacy-enigma-cli-releases/releases/download/{{.Version}}/codacy-enigma-cli-releases_{{.Version}}_{{.OS}}_{{.Arch}}.tar.gz"
5+
binaries:
6+
- name: codacy-enigma-cli
7+
path: "codacy-enigma-cli-releases"
8+
formatters:
9+
- name: json
10+
flag: "-f json"
11+
- name: sarif
12+
flag: "-f sarif"

tools/enigmaRunner.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package tools
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"path/filepath"
8+
)
9+
10+
func RunEnigma(workDirectory string, installationDirectory string, binary string, files []string, outputFile string, outputFormat string) error {
11+
12+
configFiles := []string{"enigma.yaml", "enigma.yml"}
13+
14+
if outputFormat == "" {
15+
outputFormat = "text"
16+
}
17+
18+
args := []string{"analyze", "--format", outputFormat}
19+
20+
if len(files) > 0 {
21+
args = append(args, append([]string{"--paths"}, files...)...)
22+
} else {
23+
args = append(args, "--paths", ".")
24+
}
25+
26+
configExists := ""
27+
for _, configFile := range configFiles {
28+
if _, err := os.Stat(filepath.Join(workDirectory, configFile)); err == nil {
29+
configExists = filepath.Join(workDirectory, configFile)
30+
break
31+
}
32+
}
33+
34+
if configExists != "" {
35+
println("Config file found, using it")
36+
args = append(args, "--configuration-file", configExists)
37+
} else {
38+
println("No config file found, using tool defaults")
39+
40+
}
41+
42+
cmd := exec.Command(binary, args...)
43+
cmd.Dir = workDirectory
44+
cmd.Stderr = os.Stderr
45+
if outputFile != "" {
46+
// If output file is specified, create it and redirect output
47+
var outputWriter *os.File
48+
var err error
49+
outputWriter, err = os.Create(filepath.Clean(outputFile))
50+
if err != nil {
51+
return fmt.Errorf("failed to create output file: %w", err)
52+
}
53+
defer outputWriter.Close()
54+
cmd.Stdout = outputWriter
55+
} else {
56+
cmd.Stdout = os.Stdout
57+
}
58+
err := cmd.Run()
59+
if err != nil {
60+
return fmt.Errorf("failed to run Enigma: %w", err)
61+
}
62+
return nil
63+
}

tools/enigmaRunner_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package tools
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestRunEnigma_NoConfig_NoOutputFile(t *testing.T) {
13+
tempDir := t.TempDir()
14+
fakeBinary := "/bin/echo"
15+
err := RunEnigma(tempDir, tempDir, fakeBinary, []string{"foo.go"}, "", "text")
16+
assert.NoError(t, err)
17+
}
18+
19+
func TestRunEnigma_WithConfig_NoOutputFile(t *testing.T) {
20+
tempDir := t.TempDir()
21+
configPath := filepath.Join(tempDir, "enigma.yaml")
22+
ioutil.WriteFile(configPath, []byte("config: true"), 0644)
23+
fakeBinary := "/bin/echo"
24+
err := RunEnigma(tempDir, tempDir, fakeBinary, []string{"foo.go"}, "", "text")
25+
assert.NoError(t, err)
26+
}
27+
28+
func TestRunEnigma_NoConfig_WithOutputFile(t *testing.T) {
29+
tempDir := t.TempDir()
30+
outputFile := filepath.Join(tempDir, "output.txt")
31+
fakeBinary := "/bin/echo"
32+
err := RunEnigma(tempDir, tempDir, fakeBinary, []string{"foo.go"}, outputFile, "text")
33+
assert.NoError(t, err)
34+
_, err = os.Stat(outputFile)
35+
assert.NoError(t, err)
36+
}
37+
38+
func TestRunEnigma_WithConfig_WithOutputFile(t *testing.T) {
39+
tempDir := t.TempDir()
40+
configPath := filepath.Join(tempDir, "enigma.yaml")
41+
ioutil.WriteFile(configPath, []byte("config: true"), 0644)
42+
outputFile := filepath.Join(tempDir, "output.txt")
43+
fakeBinary := "/bin/echo"
44+
err := RunEnigma(tempDir, tempDir, fakeBinary, []string{"foo.go"}, outputFile, "text")
45+
assert.NoError(t, err)
46+
_, err = os.Stat(outputFile)
47+
assert.NoError(t, err)
48+
}
49+
50+
func TestRunEnigma_CreateOutputFileError(t *testing.T) {
51+
tempDir := t.TempDir()
52+
fakeBinary := "/bin/echo"
53+
// Use a directory as output file to force error
54+
outputFile := tempDir
55+
err := RunEnigma(tempDir, tempDir, fakeBinary, []string{"foo.go"}, outputFile, "text")
56+
assert.Error(t, err)
57+
}

0 commit comments

Comments
 (0)