Skip to content

Commit 8e1ef85

Browse files
wip: some test fails
1 parent 3e978a2 commit 8e1ef85

File tree

7 files changed

+719
-85
lines changed

7 files changed

+719
-85
lines changed

.cursor/rules/cursor.mdc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ alwaysApply: true
1212
- run go build after each code modification to see if app compiles
1313
- remove dead unused code
1414
- look for constants like file permissons in `constants` folder
15+
- run go tests if you modified tests files
1516

1617
## Code Style Guidelines
1718
- **Imports**: Standard lib first, external packages second, internal last

cmd/init.go

Lines changed: 134 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,107 @@ var initCmd = &cobra.Command{
4747

4848
if cliLocalMode {
4949
fmt.Println()
50-
fmt.Println("ℹ️ No project token was specified, fetching codacy default configurations")
51-
noTools := []domain.Tool{}
52-
err := createConfigurationFiles(noTools, cliLocalMode)
50+
fmt.Println("ℹ️ No project token was specified, detecting languages and configuring tools...")
51+
52+
// Create language detector and scan the project
53+
detector := domain.NewLanguageDetector()
54+
languages, err := detector.DetectLanguages(".")
55+
if err != nil {
56+
log.Fatalf("Failed to detect languages: %v", err)
57+
}
58+
59+
// Map detected languages to tools
60+
var enabledTools []domain.Tool
61+
toolVersions := map[string]string{
62+
ESLint: "8.57.0",
63+
Trivy: "0.59.1",
64+
PyLint: "3.3.6",
65+
PMD: "6.55.0",
66+
DartAnalyzer: "3.7.2",
67+
Semgrep: "1.78.0",
68+
Lizard: "1.17.19",
69+
}
70+
71+
// Map languages to tools
72+
languageToTools := map[string][]string{
73+
"JavaScript": {ESLint},
74+
"TypeScript": {ESLint},
75+
"Python": {PyLint},
76+
"Java": {PMD},
77+
"Dart": {DartAnalyzer},
78+
"Go": {Semgrep},
79+
"Ruby": {Semgrep},
80+
"PHP": {Semgrep},
81+
"C": {Semgrep},
82+
"C++": {Semgrep},
83+
"C#": {Semgrep},
84+
"Kotlin": {Semgrep},
85+
"Swift": {Semgrep},
86+
"Scala": {PMD},
87+
"Rust": {Semgrep},
88+
}
89+
90+
// Always enable Trivy for security scanning
91+
enabledTools = append(enabledTools, domain.Tool{
92+
Uuid: Trivy,
93+
Name: "trivy",
94+
Version: toolVersions[Trivy],
95+
})
96+
97+
// Enable tools based on detected languages
98+
toolsEnabled := make(map[string]bool)
99+
for langName := range languages {
100+
if tools, ok := languageToTools[langName]; ok {
101+
for _, toolID := range tools {
102+
if !toolsEnabled[toolID] {
103+
toolsEnabled[toolID] = true
104+
enabledTools = append(enabledTools, domain.Tool{
105+
Uuid: toolID,
106+
Name: toolNameFromUUID(toolID),
107+
Version: toolVersions[toolID],
108+
})
109+
}
110+
}
111+
}
112+
}
113+
114+
// Always enable Lizard for complexity analysis if any supported language is detected
115+
if shouldEnableLizard(languages) {
116+
enabledTools = append(enabledTools, domain.Tool{
117+
Uuid: Lizard,
118+
Name: "lizard",
119+
Version: toolVersions[Lizard],
120+
})
121+
}
122+
123+
// Create configuration files
124+
err = createConfigurationFiles(enabledTools, cliLocalMode)
53125
if err != nil {
54126
log.Fatal(err)
55127
}
56-
// Create default configuration files
57-
if err := buildDefaultConfigurationFiles(toolsConfigDir); err != nil {
128+
129+
// Create default configuration files for enabled tools
130+
if err := buildDefaultConfigurationFiles(toolsConfigDir, enabledTools); err != nil {
58131
log.Fatal(err)
59132
}
133+
134+
// Print summary of detected languages and enabled tools
135+
fmt.Println("\nDetected languages:")
136+
for langName, info := range languages {
137+
fmt.Printf(" - %s (%d files)\n", langName, len(info.Files))
138+
}
139+
140+
fmt.Println("\nEnabled tools:")
141+
for _, tool := range enabledTools {
142+
fmt.Printf(" - %s@%s\n", tool.Name, tool.Version)
143+
}
60144
} else {
61145
err := buildRepositoryConfigurationFiles(initFlags.ApiToken)
62146
if err != nil {
63147
log.Fatal(err)
64148
}
65149
}
150+
66151
createGitIgnoreFile()
67152
fmt.Println()
68153
fmt.Println("✅ Successfully initialized Codacy configuration!")
@@ -74,6 +159,45 @@ var initCmd = &cobra.Command{
74159
},
75160
}
76161

162+
// shouldEnableLizard checks if Lizard should be enabled based on detected languages
163+
func shouldEnableLizard(languages map[string]*domain.LanguageInfo) bool {
164+
lizardSupportedLangs := map[string]bool{
165+
"C": true, "C++": true, "Java": true, "C#": true,
166+
"JavaScript": true, "TypeScript": true, "Python": true,
167+
"Ruby": true, "PHP": true, "Scala": true, "Go": true,
168+
"Rust": true, "Kotlin": true, "Swift": true,
169+
}
170+
171+
for lang := range languages {
172+
if lizardSupportedLangs[lang] {
173+
return true
174+
}
175+
}
176+
return false
177+
}
178+
179+
// toolNameFromUUID returns the short name for a tool UUID
180+
func toolNameFromUUID(uuid string) string {
181+
switch uuid {
182+
case ESLint:
183+
return "eslint"
184+
case Trivy:
185+
return "trivy"
186+
case PyLint:
187+
return "pylint"
188+
case PMD:
189+
return "pmd"
190+
case DartAnalyzer:
191+
return "dartanalyzer"
192+
case Semgrep:
193+
return "semgrep"
194+
case Lizard:
195+
return "lizard"
196+
default:
197+
return "unknown"
198+
}
199+
}
200+
77201
func createGitIgnoreFile() error {
78202
gitIgnorePath := filepath.Join(config.Config.LocalCodacyDirectory(), ".gitignore")
79203
gitIgnoreFile, err := os.Create(gitIgnorePath)
@@ -427,14 +551,14 @@ func createLizardConfigFile(toolsConfigDir string, patternConfiguration []domain
427551
return nil
428552
}
429553

430-
// buildDefaultConfigurationFiles creates default configuration files for all tools
431-
func buildDefaultConfigurationFiles(toolsConfigDir string) error {
432-
for _, tool := range AvailableTools {
433-
patternsConfig, err := codacyclient.GetDefaultToolPatternsConfig(initFlags, tool)
554+
// buildDefaultConfigurationFiles creates default configuration files for enabled tools
555+
func buildDefaultConfigurationFiles(toolsConfigDir string, enabledTools []domain.Tool) error {
556+
for _, tool := range enabledTools {
557+
patternsConfig, err := codacyclient.GetDefaultToolPatternsConfig(initFlags, tool.Uuid)
434558
if err != nil {
435559
return fmt.Errorf("failed to get default tool patterns config: %w", err)
436560
}
437-
switch tool {
561+
switch tool.Uuid {
438562
case ESLint:
439563
if err := tools.CreateEslintConfig(toolsConfigDir, patternsConfig); err != nil {
440564
return fmt.Errorf("failed to create eslint config file: %v", err)

0 commit comments

Comments
 (0)