Skip to content

Commit faa8566

Browse files
Feature: config discover command (#142) [PLUTO-1429]
- Added functionality to detect file extensions in the specified path before language detection. - Implemented logging for recognized file extensions and detected languages using the logger package. - Improved error handling for invalid configurations in the Codacy YAML file.
1 parent 63a2be3 commit faa8566

File tree

10 files changed

+911
-100
lines changed

10 files changed

+911
-100
lines changed

README.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,46 @@ Bootstraps the CLI configuration in your project's folder. This command creates
7171
- `--organization` (string): Organization name, required with `--api-token`
7272
- `--repository` (string): Repository name, required with `--api-token`
7373

74+
### `config reset` — Reset Configuration
75+
76+
Resets the Codacy configuration files and tool-specific configurations. This command overwrites existing configuration with either local default configurations or repository-specific configurations from Codacy.
77+
78+
```bash
79+
# Reset to local default configurations
80+
codacy-cli config reset
81+
82+
# Reset to repository-specific configurations from Codacy
83+
codacy-cli config reset --api-token <token> --provider <gh|gl|bb> --organization <org> --repository <repo>
84+
```
85+
86+
**Behavior:**
87+
- **Local mode**: Creates default configurations for all available tools
88+
- **Remote mode**: Fetches and applies repository-specific configurations from Codacy
89+
- Prevents accidental mode switching (remote to local requires explicit flags)
90+
- Overwrites existing `.codacy/codacy.yaml` and tool configurations
91+
- Creates or updates `.codacy/.gitignore` file
92+
93+
**Flags:** Same as `init` command (api-token, provider, organization, repository)
94+
95+
### `config discover` — Discover Project Languages
96+
97+
Scans a project directory to detect programming languages and automatically configures appropriate static analysis tools. This command updates both `languages-config.yaml` and `codacy.yaml` with relevant tools for detected languages.
98+
99+
```bash
100+
# Discover languages in current directory
101+
codacy-cli config discover .
102+
103+
# Discover languages in specific project path
104+
codacy-cli config discover /path/to/project
105+
```
106+
107+
**Features:**
108+
- Automatically detects file extensions and maps them to programming languages
109+
- Updates `.codacy/tools-configs/languages-config.yaml` with discovered languages
110+
- Enables relevant tools in `codacy.yaml` based on detected languages
111+
- Creates tool-specific configuration files for discovered tools
112+
- Works in both local and cloud modes
113+
74114
### `install` — Install Runtimes and Tools
75115

76116
Installs all runtimes and tools specified in `.codacy/codacy.yaml`:
@@ -172,14 +212,17 @@ codacy-cli init
172212
# or
173213
codacy-cli init --api-token <token> --provider gh --organization my-org --repository my-repo
174214

175-
# 2. Install all required runtimes and tools
215+
# 2. (Optional) Discover languages and configure tools automatically
216+
codacy-cli config discover .
217+
218+
# 3. Install all required runtimes and tools
176219
codacy-cli install
177220

178-
# 3. Run analysis (all tools or specific tool)
221+
# 4. Run analysis (all tools or specific tool)
179222
codacy-cli analyze
180223
codacy-cli analyze --tool eslint
181224

182-
# 4. Upload results to Codacy
225+
# 5. Upload results to Codacy
183226
codacy-cli upload -s eslint.sarif -c <commit-uuid> -t <project-token>
184227
```
185228

cli-v2.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ func main() {
3939
}
4040
}
4141

42-
// Check if command is init/update
43-
if len(os.Args) > 1 && (os.Args[1] == "init" || os.Args[1] == "update") {
44-
cmd.Execute()
45-
return
42+
// Check if command is init/update/version/help - these don't require configuration
43+
if len(os.Args) > 1 {
44+
cmdName := os.Args[1]
45+
if cmdName == "init" || cmdName == "update" || cmdName == "version" || cmdName == "help" {
46+
cmd.Execute()
47+
return
48+
}
4649
}
4750

4851
// All other commands require a configuration file

cmd/cmdutils/flags.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cmdutils
2+
3+
import (
4+
"codacy/cli-v2/domain"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// AddCloudFlags adds the common cloud-related flags to a cobra command.
10+
// The flags will be bound to the provided flags struct.
11+
func AddCloudFlags(cmd *cobra.Command, flags *domain.InitFlags) {
12+
cmd.Flags().StringVar(&flags.ApiToken, "api-token", "", "Optional Codacy API token. If defined, configurations will be fetched from Codacy")
13+
cmd.Flags().StringVar(&flags.Provider, "provider", "", "Provider (e.g., gh, bb, gl) to fetch configurations from Codacy. Required when api-token is provided")
14+
cmd.Flags().StringVar(&flags.Organization, "organization", "", "Remote organization name to fetch configurations from Codacy. Required when api-token is provided")
15+
cmd.Flags().StringVar(&flags.Repository, "repository", "", "Remote repository name to fetch configurations from Codacy. Required when api-token is provided")
16+
}

0 commit comments

Comments
 (0)