Skip to content

Commit 121df59

Browse files
wip
1 parent edd7158 commit 121df59

File tree

8 files changed

+82
-2
lines changed

8 files changed

+82
-2
lines changed

cmd/analyze.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,21 @@ func runTool(workDirectory string, toolName string, pathsToCheck []string, outpu
366366
runtime = config.Config.Runtimes()[tool.Runtime]
367367
}
368368
}
369+
370+
if tool.ConfigFileName != "" {
371+
configFile, exists := HasConfigFile(config.Config, tool.ConfigFileName)
372+
if !exists {
373+
fmt.Printf("Config file %s not found, creating...\n", tool.ConfigFileName)
374+
// TODO: Implement CreateConfigFile function in utils
375+
// err := utils.CreateConfigFile(config.Config, tool.ConfigFileName)
376+
// if err != nil {
377+
// return fmt.Errorf("failed to create config file %s: %w", tool.ConfigFileName, err)
378+
// }
379+
} else {
380+
fmt.Printf("Using existing config file: %s\n", configFile)
381+
}
382+
}
383+
369384
return runToolByName(toolName, workDirectory, pathsToCheck, autoFix, outputFile, outputFormat, tool, runtime)
370385
}
371386

@@ -456,3 +471,56 @@ var analyzeCmd = &cobra.Command{
456471
}
457472
},
458473
}
474+
475+
// HasConfigFile checks if a tool's config file exists in the .codacy/tools-configs directory
476+
// or in the repository root directory.
477+
//
478+
// Parameters:
479+
// - config: The configuration object containing directory paths
480+
// - configFileName: The name of the config file to check for
481+
//
482+
// Returns:
483+
// - string: The full path to the config file if it exists
484+
// - bool: True if the config file exists, false otherwise
485+
func HasConfigFile(config interface{}, configFileName string) (string, bool) {
486+
fmt.Println("Checking for config file:", configFileName)
487+
// If no config file name is specified, return false
488+
if configFileName == "" {
489+
return "", false
490+
}
491+
492+
// We need to get the workspace directory from the config
493+
// For now, we'll check in the current directory and .codacy/tools-config
494+
workDir, err := os.Getwd()
495+
if err != nil {
496+
return "", false
497+
}
498+
499+
fmt.Println("Work directory:", workDir)
500+
fmt.Println("Checking in .codacy/tools-configs directory first")
501+
// Check in .codacy/tools-configs directory first
502+
toolsConfigDir := filepath.Join(workDir, ".codacy", "tools-configs")
503+
fmt.Println("Tools config directory:", toolsConfigDir)
504+
configPath := filepath.Join(toolsConfigDir, configFileName)
505+
fmt.Println("Config path:", configPath)
506+
if _, err := os.Stat(configPath); err == nil {
507+
fmt.Println("Found config file in tools-configs directory!")
508+
return configPath, true
509+
} else {
510+
fmt.Println("Config file NOT found in tools-configs directory. Error:", err)
511+
}
512+
513+
// Check in repository root directory as fallback
514+
rootConfigPath := filepath.Join(workDir, configFileName)
515+
fmt.Println("Checking root config path:", rootConfigPath)
516+
if _, err := os.Stat(rootConfigPath); err == nil {
517+
fmt.Println("Found config file in repository root directory!")
518+
return rootConfigPath, true
519+
} else {
520+
fmt.Println("Config file NOT found in repository root. Error:", err)
521+
}
522+
523+
// Config file not found
524+
fmt.Println("Config file not found anywhere")
525+
return "", false
526+
}

plugins/tool-utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type ToolPluginConfig struct {
6767
Formatters []Formatter `yaml:"formatters"`
6868
OutputOptions OutputOptions `yaml:"output_options"`
6969
AnalysisOptions AnalysisOptions `yaml:"analysis_options"`
70+
ConfigFileName string `yaml:"config_file_name,omitempty"` // Optional field
7071
}
7172

7273
// ToolConfig represents configuration for a tool
@@ -99,6 +100,8 @@ type ToolInfo struct {
99100
Extension string
100101
// Environment variables
101102
Environment map[string]string
103+
// Config file
104+
ConfigFileName string
102105
}
103106

104107
// ProcessTools processes a list of tool configurations and returns a map of tool information
@@ -152,6 +155,8 @@ func ProcessTools(configs []ToolConfig, toolDir string, runtimes map[string]*Run
152155
RegistryCommand: pluginConfig.Installation.RegistryTemplate,
153156
// Store environment variables
154157
Environment: make(map[string]string),
158+
// Store config file name
159+
ConfigFileName: pluginConfig.ConfigFileName,
155160
}
156161

157162
// Handle download configuration for directly downloaded tools

plugins/tools/eslint/plugin.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ output_options:
1919
analysis_options:
2020
autofix_flag: "--fix"
2121
default_path: "."
22+
uses_config: true
23+
config_file_name: "eslint.config.mjs"

plugins/tools/lizard/plugin.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ binaries:
1111
output_options:
1212
file_flag: "-o"
1313
analysis_options:
14-
default_path: "."
14+
default_path: "."
15+
config_file_name: "lizard.yaml"

plugins/tools/pmd/plugin.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ environment:
1515
binaries:
1616
- name: pmd
1717
path: "pmd-bin-{{.Version}}/bin/{{if ge .Version \"7.0.0\"}}pmd{{else}}run.sh{{end}}"
18+
config_file_name: "ruleset.xml"

plugins/tools/pylint/plugin.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ formatters:
1414
output_options:
1515
file_flag: "--output"
1616
analysis_options:
17-
default_path: "."
17+
default_path: "."
18+
config_file_name: "pylint.rc"

plugins/tools/semgrep/plugin.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ output_options:
1515
file_flag: "--output"
1616
analysis_options:
1717
default_path: "."
18+
config_file_name: "semgrep.yaml"

plugins/tools/trivy/plugin.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ download:
2020
binaries:
2121
- name: trivy
2222
path: "trivy"
23+
config_file_name: "trivy.yaml"

0 commit comments

Comments
 (0)