11package cmd
22
33import (
4+ "codacy/cli-v2/cmd/cmdutils"
5+ "codacy/cli-v2/cmd/configsetup"
46 "codacy/cli-v2/config"
57 "codacy/cli-v2/domain"
68 "codacy/cli-v2/plugins"
@@ -39,6 +41,18 @@ type LanguagesConfig struct {
3941 } `yaml:"tools" json:"tools"`
4042}
4143
44+ // toolConfigFileName maps tool names to their configuration filenames
45+ var toolConfigFileName = map [string ]string {
46+ "eslint" : "eslint.config.mjs" ,
47+ "trivy" : "trivy.yaml" ,
48+ "pmd" : "ruleset.xml" ,
49+ "pylint" : "pylint.rc" ,
50+ "dartanalyzer" : "analysis_options.yaml" ,
51+ "semgrep" : "semgrep.yaml" ,
52+ "revive" : "revive.toml" ,
53+ "lizard" : "lizard.yaml" ,
54+ }
55+
4256// LoadLanguageConfig loads the language configuration from the file
4357func LoadLanguageConfig () (* LanguagesConfig , error ) {
4458 // First, try to load the YAML config
@@ -217,6 +231,7 @@ func init() {
217231 analyzeCmd .Flags ().StringVarP (& toolsToAnalyzeParam , "tool" , "t" , "" , "Which tool to run analysis with. If not specified, all configured tools will be run" )
218232 analyzeCmd .Flags ().StringVar (& outputFormat , "format" , "" , "Output format (use 'sarif' for SARIF format)" )
219233 analyzeCmd .Flags ().BoolVar (& autoFix , "fix" , false , "Apply auto fix to your issues when available" )
234+ cmdutils .AddCloudFlags (analyzeCmd , & initFlags )
220235 rootCmd .AddCommand (analyzeCmd )
221236}
222237
@@ -277,7 +292,41 @@ func validateToolName(toolName string) error {
277292 return nil
278293}
279294
280- func runToolByName (toolName string , workDirectory string , pathsToCheck []string , autoFix bool , outputFile string , outputFormat string , tool * plugins.ToolInfo , runtime * plugins.RuntimeInfo ) error {
295+ func checkIfConfigExistsAndIsNeeded (toolName string , cliLocalMode bool ) error {
296+ configFileName := toolConfigFileName [toolName ]
297+ if configFileName == "" {
298+ // Tool doesn't use config file
299+ return nil
300+ }
301+
302+ // Use the configuration system to get the tools config directory
303+ toolsConfigDir := config .Config .ToolsConfigDirectory ()
304+ toolConfigPath := filepath .Join (toolsConfigDir , configFileName )
305+
306+ // Check if the config file exists
307+ if _ , err := os .Stat (toolConfigPath ); os .IsNotExist (err ) {
308+ // Only show error if we're in remote mode and need the config file
309+ if ! cliLocalMode && initFlags .ApiToken != "" {
310+ fmt .Printf ("Creating new config file for tool %s\n " , toolName )
311+ configsetup .CreateToolConfigurationFile (toolName , initFlags )
312+ } else if ! cliLocalMode {
313+ fmt .Printf ("config file not found for tool %s: %s and no api token provided \n " , toolName , toolConfigPath )
314+ } else {
315+ fmt .Printf ("config file not found for tool %s: %s please add a config file to the tools-configs directory\n " , toolName , toolConfigPath )
316+ }
317+ } else if err != nil {
318+ fmt .Printf ("error checking config file for tool %s: %v\n " , toolName , err )
319+ } else {
320+ fmt .Printf ("Config file found for %s: %s\n " , toolName , toolConfigPath )
321+ }
322+ return nil
323+ }
324+
325+ func runToolByName (toolName string , workDirectory string , pathsToCheck []string , autoFix bool , outputFile string , outputFormat string , tool * plugins.ToolInfo , runtime * plugins.RuntimeInfo , cliLocalMode bool ) error {
326+ err := checkIfConfigExistsAndIsNeeded (toolName , cliLocalMode )
327+ if err != nil {
328+ return err
329+ }
281330 switch toolName {
282331 case "eslint" :
283332 binaryPath := runtime .Binaries [tool .Runtime ]
@@ -308,7 +357,7 @@ func runToolByName(toolName string, workDirectory string, pathsToCheck []string,
308357 return fmt .Errorf ("unsupported tool: %s" , toolName )
309358}
310359
311- func runTool (workDirectory string , toolName string , pathsToCheck []string , outputFile string , autoFix bool , outputFormat string ) error {
360+ func runTool (workDirectory string , toolName string , pathsToCheck []string , outputFile string , autoFix bool , outputFormat string , cliLocalMode bool ) error {
312361 err := validateToolName (toolName )
313362 if err != nil {
314363 return err
@@ -366,7 +415,7 @@ func runTool(workDirectory string, toolName string, pathsToCheck []string, outpu
366415 runtime = config .Config .Runtimes ()[tool .Runtime ]
367416 }
368417 }
369- return runToolByName (toolName , workDirectory , pathsToCheck , autoFix , outputFile , outputFormat , tool , runtime )
418+ return runToolByName (toolName , workDirectory , pathsToCheck , autoFix , outputFile , outputFormat , tool , runtime , cliLocalMode )
370419}
371420
372421// validatePaths checks if all provided paths exist and returns an error if any don't
@@ -379,6 +428,13 @@ func validatePaths(paths []string) error {
379428 return nil
380429}
381430
431+ func validateCloudMode (cliLocalMode bool ) error {
432+ if cliLocalMode {
433+ fmt .Println ("Warning: cannot run in cloud mode" )
434+ }
435+ return nil
436+ }
437+
382438var analyzeCmd = & cobra.Command {
383439 Use : "analyze" ,
384440 Short : "Analyze code using configured tools" ,
@@ -396,6 +452,10 @@ var analyzeCmd = &cobra.Command{
396452 log .Fatalf ("Failed to get current working directory: %v" , err )
397453 }
398454
455+ cliLocalMode := len (initFlags .ApiToken ) == 0
456+
457+ validateCloudMode (cliLocalMode )
458+
399459 var toolsToRun map [string ]* plugins.ToolInfo
400460
401461 if toolsToAnalyzeParam != "" {
@@ -432,7 +492,7 @@ var analyzeCmd = &cobra.Command{
432492 var sarifOutputs []string
433493 for toolName := range toolsToRun {
434494 tmpFile := filepath .Join (tmpDir , fmt .Sprintf ("%s.sarif" , toolName ))
435- if err := runTool (workDirectory , toolName , args , tmpFile , autoFix , outputFormat ); err != nil {
495+ if err := runTool (workDirectory , toolName , args , tmpFile , autoFix , outputFormat , cliLocalMode ); err != nil {
436496 log .Printf ("Tool failed to run: %v\n " , err )
437497 }
438498 sarifOutputs = append (sarifOutputs , tmpFile )
@@ -467,7 +527,7 @@ var analyzeCmd = &cobra.Command{
467527 } else {
468528 // Run tools without merging outputs
469529 for toolName := range toolsToRun {
470- if err := runTool (workDirectory , toolName , args , outputFile , autoFix , outputFormat ); err != nil {
530+ if err := runTool (workDirectory , toolName , args , outputFile , autoFix , outputFormat , cliLocalMode ); err != nil {
471531 log .Printf ("Tool failed to run: %v\n " , err )
472532 }
473533 }
0 commit comments