Skip to content

Commit 03e3bf4

Browse files
committed
feature: Only create configurations for tools that use UI config PLUTO-1384
1 parent 0142cac commit 03e3bf4

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

cmd/init.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ func buildRepositoryConfigurationFiles(token string) error {
183183
return fmt.Errorf("failed to create tools-configs directory: %w", err)
184184
}
185185

186+
// Clear any previous configuration files
187+
if err := cleanConfigDirectory(toolsConfigDir); err != nil {
188+
return fmt.Errorf("failed to clean configuration directory: %w", err)
189+
}
190+
186191
client := &http.Client{
187192
Timeout: 10 * time.Second,
188193
}
@@ -192,12 +197,24 @@ func buildRepositoryConfigurationFiles(token string) error {
192197
return err
193198
}
194199

195-
err = createConfigurationFiles(apiTools, true)
200+
// Filter out any tools that use configuration file
201+
var configuredToolsWithUI []tools.Tool
202+
for _, tool := range apiTools {
203+
if !tool.Settings.UsesConfigFile {
204+
configuredToolsWithUI = append(configuredToolsWithUI, tool)
205+
} else {
206+
fmt.Printf("Skipping config generation for %s - configured to use repo's config file\n", tool.Name)
207+
}
208+
}
209+
210+
// Create main config files with all enabled API tools
211+
err = createConfigurationFiles(apiTools, false)
196212
if err != nil {
197213
log.Fatal(err)
198214
}
199215

200-
for _, tool := range apiTools {
216+
// Only generate config files for tools not using their own config file
217+
for _, tool := range configuredToolsWithUI {
201218
url := fmt.Sprintf("%s/api/v3/analysis/organizations/%s/%s/repositories/%s/tools/%s/patterns?enabled=true",
202219
CodacyApiBase,
203220
initFlags.provider,
@@ -375,6 +392,33 @@ func createDefaultEslintConfigFile(toolsConfigDir string) error {
375392
return os.WriteFile(filepath.Join(toolsConfigDir, "eslint.config.mjs"), []byte(content), utils.DefaultFilePerms)
376393
}
377394

395+
// cleanConfigDirectory removes all previous configuration files in the tools-configs directory
396+
func cleanConfigDirectory(toolsConfigDir string) error {
397+
// Check if directory exists
398+
if _, err := os.Stat(toolsConfigDir); os.IsNotExist(err) {
399+
return nil // Directory doesn't exist, nothing to clean
400+
}
401+
402+
// Read directory contents
403+
entries, err := os.ReadDir(toolsConfigDir)
404+
if err != nil {
405+
return fmt.Errorf("failed to read config directory: %w", err)
406+
}
407+
408+
// Remove all files
409+
for _, entry := range entries {
410+
if !entry.IsDir() { // Only remove files, not subdirectories
411+
filePath := filepath.Join(toolsConfigDir, entry.Name())
412+
if err := os.Remove(filePath); err != nil {
413+
return fmt.Errorf("failed to remove file %s: %w", filePath, err)
414+
}
415+
}
416+
}
417+
418+
fmt.Println("Cleaned previous configuration files")
419+
return nil
420+
}
421+
378422
const (
379423
ESLint string = "f8b29663-2cb2-498d-b923-a10c6a8c05cd"
380424
Trivy string = "2fd7fbe0-33f9-4ab3-ab73-e9b62404e2cb"

0 commit comments

Comments
 (0)