Skip to content

Commit 5c0f1a6

Browse files
review improvements
- Renamed `toolToAnalyze` to `toolsToAnalyzeParam` for clarity. - Updated the logic to allow specifying a single tool for analysis or running all configured tools if none is specified. - Improved error handling for cases with no configured tools.
1 parent 08bade6 commit 5c0f1a6

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

cmd/analyze.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"codacy/cli-v2/config"
5+
"codacy/cli-v2/plugins"
56
"codacy/cli-v2/tools"
67
"encoding/json"
78
"fmt"
@@ -17,7 +18,7 @@ import (
1718
)
1819

1920
var outputFile string
20-
var toolToAnalyze string
21+
var toolsToAnalyzeParam string
2122
var autoFix bool
2223
var outputFormat string
2324
var sarifPath string
@@ -95,7 +96,7 @@ type Pattern struct {
9596

9697
func init() {
9798
analyzeCmd.Flags().StringVarP(&outputFile, "output", "o", "", "Output file for analysis results")
98-
analyzeCmd.Flags().StringVarP(&toolToAnalyze, "tool", "t", "", "Optional: Specific tool to run analysis with. If not specified, all configured tools will be run")
99+
analyzeCmd.Flags().StringVarP(&toolsToAnalyzeParam, "tool", "t", "", "Which tool to run analysis with. If not specified, all configured tools will be run")
99100
analyzeCmd.Flags().StringVar(&outputFormat, "format", "", "Output format (use 'sarif' for SARIF format)")
100101
analyzeCmd.Flags().BoolVar(&autoFix, "fix", false, "Apply auto fix to your issues when available")
101102
rootCmd.AddCommand(analyzeCmd)
@@ -234,17 +235,19 @@ var analyzeCmd = &cobra.Command{
234235
if err != nil {
235236
log.Fatal(err)
236237
}
238+
var toolsToRun map[string]*plugins.ToolInfo
237239

238-
// If a specific tool is specified, only run that tool
239-
if toolToAnalyze != "" {
240-
log.Printf("Running %s...\n", toolToAnalyze)
241-
runTool(workDirectory, toolToAnalyze, args, outputFile)
242-
return
240+
if toolsToAnalyzeParam != "" {
241+
// If a specific tool is specified, only run that tool
242+
toolsToRun = map[string]*plugins.ToolInfo{
243+
toolsToAnalyzeParam: config.Config.Tools()[toolsToAnalyzeParam],
244+
}
245+
} else {
246+
// Run all configured tools
247+
toolsToRun = config.Config.Tools()
243248
}
244249

245-
// Run all configured tools
246-
tools := config.Config.Tools()
247-
if len(tools) == 0 {
250+
if len(toolsToRun) == 0 {
248251
log.Fatal("No tools configured. Please run 'codacy-cli init' and 'codacy-cli install' first")
249252
}
250253

@@ -259,7 +262,7 @@ var analyzeCmd = &cobra.Command{
259262
defer os.RemoveAll(tmpDir)
260263

261264
var sarifOutputs []string
262-
for toolName := range tools {
265+
for toolName := range toolsToRun {
263266
log.Printf("Running %s...\n", toolName)
264267
tmpFile := filepath.Join(tmpDir, fmt.Sprintf("%s.sarif", toolName))
265268
runTool(workDirectory, toolName, args, tmpFile)
@@ -291,7 +294,7 @@ var analyzeCmd = &cobra.Command{
291294
}
292295
} else {
293296
// Run tools without merging outputs
294-
for toolName := range tools {
297+
for toolName := range toolsToRun {
295298
log.Printf("Running %s...\n", toolName)
296299
runTool(workDirectory, toolName, args, outputFile)
297300
}

0 commit comments

Comments
 (0)