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"
@@ -41,6 +43,18 @@ type LanguagesConfig struct {
4143 } `yaml:"tools" json:"tools"`
4244}
4345
46+ // toolConfigFileName maps tool names to their configuration filenames
47+ var toolConfigFileName = map [string ]string {
48+ "eslint" : "eslint.config.mjs" ,
49+ "trivy" : "trivy.yaml" ,
50+ "pmd" : "ruleset.xml" ,
51+ "pylint" : "pylint.rc" ,
52+ "dartanalyzer" : "analysis_options.yaml" ,
53+ "semgrep" : "semgrep.yaml" ,
54+ "revive" : "revive.toml" ,
55+ "lizard" : "lizard.yaml" ,
56+ }
57+
4458// LoadLanguageConfig loads the language configuration from the file
4559func LoadLanguageConfig () (* LanguagesConfig , error ) {
4660 // First, try to load the YAML config
@@ -219,6 +233,7 @@ func init() {
219233 analyzeCmd .Flags ().StringVarP (& toolsToAnalyzeParam , "tool" , "t" , "" , "Which tool to run analysis with. If not specified, all configured tools will be run" )
220234 analyzeCmd .Flags ().StringVar (& outputFormat , "format" , "" , "Output format (use 'sarif' for SARIF format)" )
221235 analyzeCmd .Flags ().BoolVar (& autoFix , "fix" , false , "Apply auto fix to your issues when available" )
236+ cmdutils .AddCloudFlags (analyzeCmd , & initFlags )
222237 rootCmd .AddCommand (analyzeCmd )
223238}
224239
@@ -279,7 +294,41 @@ func validateToolName(toolName string) error {
279294 return nil
280295}
281296
282- func runToolByName (toolName string , workDirectory string , pathsToCheck []string , autoFix bool , outputFile string , outputFormat string , tool * plugins.ToolInfo , runtime * plugins.RuntimeInfo ) error {
297+ func checkIfConfigExistsAndIsNeeded (toolName string , cliLocalMode bool ) error {
298+ configFileName := toolConfigFileName [toolName ]
299+ if configFileName == "" {
300+ // Tool doesn't use config file
301+ return nil
302+ }
303+
304+ // Use the configuration system to get the tools config directory
305+ toolsConfigDir := config .Config .ToolsConfigDirectory ()
306+ toolConfigPath := filepath .Join (toolsConfigDir , configFileName )
307+
308+ // Check if the config file exists
309+ if _ , err := os .Stat (toolConfigPath ); os .IsNotExist (err ) {
310+ // Only show error if we're in remote mode and need the config file
311+ if ! cliLocalMode && initFlags .ApiToken != "" {
312+ fmt .Printf ("Creating new config file for tool %s\n " , toolName )
313+ configsetup .CreateToolConfigurationFile (toolName , initFlags )
314+ } else if ! cliLocalMode {
315+ fmt .Printf ("config file not found for tool %s: %s and no api token provided \n " , toolName , toolConfigPath )
316+ } else {
317+ fmt .Printf ("config file not found for tool %s: %s please add a config file to the tools-configs directory\n " , toolName , toolConfigPath )
318+ }
319+ } else if err != nil {
320+ fmt .Printf ("error checking config file for tool %s: %v\n " , toolName , err )
321+ } else {
322+ fmt .Printf ("Config file found for %s: %s\n " , toolName , toolConfigPath )
323+ }
324+ return nil
325+ }
326+
327+ func runToolByName (toolName string , workDirectory string , pathsToCheck []string , autoFix bool , outputFile string , outputFormat string , tool * plugins.ToolInfo , runtime * plugins.RuntimeInfo , cliLocalMode bool ) error {
328+ err := checkIfConfigExistsAndIsNeeded (toolName , cliLocalMode )
329+ if err != nil {
330+ return err
331+ }
283332 switch toolName {
284333 case "eslint" :
285334 binaryPath := runtime .Binaries [tool .Runtime ]
@@ -310,7 +359,7 @@ func runToolByName(toolName string, workDirectory string, pathsToCheck []string,
310359 return fmt .Errorf ("unsupported tool: %s" , toolName )
311360}
312361
313- func runTool (workDirectory string , toolName string , pathsToCheck []string , outputFile string , autoFix bool , outputFormat string ) error {
362+ func runTool (workDirectory string , toolName string , pathsToCheck []string , outputFile string , autoFix bool , outputFormat string , cliLocalMode bool ) error {
314363 err := validateToolName (toolName )
315364 if err != nil {
316365 return err
@@ -368,7 +417,7 @@ func runTool(workDirectory string, toolName string, pathsToCheck []string, outpu
368417 runtime = config .Config .Runtimes ()[tool .Runtime ]
369418 }
370419 }
371- return runToolByName (toolName , workDirectory , pathsToCheck , autoFix , outputFile , outputFormat , tool , runtime )
420+ return runToolByName (toolName , workDirectory , pathsToCheck , autoFix , outputFile , outputFormat , tool , runtime , cliLocalMode )
372421}
373422
374423// validatePaths checks if all provided paths exist and returns an error if any don't
@@ -384,6 +433,13 @@ func validatePaths(paths []string) error {
384433 return nil
385434}
386435
436+ func validateCloudMode (cliLocalMode bool ) error {
437+ if cliLocalMode {
438+ fmt .Println ("Warning: cannot run in cloud mode" )
439+ }
440+ return nil
441+ }
442+
387443var analyzeCmd = & cobra.Command {
388444 Use : "analyze" ,
389445 Short : "Analyze code using configured tools" ,
@@ -401,6 +457,10 @@ var analyzeCmd = &cobra.Command{
401457 log .Fatalf ("Failed to get current working directory: %v" , err )
402458 }
403459
460+ cliLocalMode := len (initFlags .ApiToken ) == 0
461+
462+ validateCloudMode (cliLocalMode )
463+
404464 var toolsToRun map [string ]* plugins.ToolInfo
405465
406466 if toolsToAnalyzeParam != "" {
@@ -437,7 +497,7 @@ var analyzeCmd = &cobra.Command{
437497 var sarifOutputs []string
438498 for toolName := range toolsToRun {
439499 tmpFile := filepath .Join (tmpDir , fmt .Sprintf ("%s.sarif" , toolName ))
440- if err := runTool (workDirectory , toolName , args , tmpFile , autoFix , outputFormat ); err != nil {
500+ if err := runTool (workDirectory , toolName , args , tmpFile , autoFix , outputFormat , cliLocalMode ); err != nil {
441501 log .Printf ("Tool failed to run: %v\n " , err )
442502 }
443503 sarifOutputs = append (sarifOutputs , tmpFile )
@@ -472,7 +532,7 @@ var analyzeCmd = &cobra.Command{
472532 } else {
473533 // Run tools without merging outputs
474534 for toolName := range toolsToRun {
475- if err := runTool (workDirectory , toolName , args , outputFile , autoFix , outputFormat ); err != nil {
535+ if err := runTool (workDirectory , toolName , args , outputFile , autoFix , outputFormat , cliLocalMode ); err != nil {
476536 log .Printf ("Tool failed to run: %v\n " , err )
477537 }
478538 }
0 commit comments