You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Introduced a new `config reset` command to reset Codacy configuration to default or repository-specific settings.
- Implemented logic to handle API token validation and ensure required flags are provided when using remote configurations.
- Added YAML parsing for CLI mode configuration from `.codacy/cli-config.yaml`.
- Updated error handling and user feedback for better clarity during configuration resets.
fmt.Println("Error: Your Codacy CLI is currently configured in 'remote' (cloud) mode.")
55
+
fmt.Println("To reset your configuration using remote settings, you must provide the --api-token, --provider, --organization, and --repository flags.")
56
+
fmt.Println("Running 'config reset' without these flags is not permitted while configured for 'remote' mode.")
57
+
fmt.Println("This prevents an accidental switch to a local default configuration.")
58
+
fmt.Println()
59
+
iferrHelp:=cmd.Help(); errHelp!=nil {
60
+
log.Printf("Warning: Failed to display command help: %v\n", errHelp)
61
+
}
62
+
os.Exit(1)
63
+
}
64
+
65
+
// Validate flags: if API token is provided, other related flags must also be provided.
// Define flags for the config update command. These are the same flags used by the init command.
97
-
configUpdateCmd.Flags().StringVar(&configUpdateInitFlags.ApiToken, "api-token", "", "Optional Codacy API token. If defined, configurations will be fetched from Codacy.")
98
-
configUpdateCmd.Flags().StringVar(&configUpdateInitFlags.Provider, "provider", "", "Provider (e.g., gh, bb, gl) to fetch configurations from Codacy. Required when api-token is provided.")
99
-
configUpdateCmd.Flags().StringVar(&configUpdateInitFlags.Organization, "organization", "", "Remote organization name to fetch configurations from Codacy. Required when api-token is provided.")
100
-
configUpdateCmd.Flags().StringVar(&configUpdateInitFlags.Repository, "repository", "", "Remote repository name to fetch configurations from Codacy. Required when api-token is provided.")
101
-
102
-
// Add the update subcommand to the config command
103
-
configCmd.AddCommand(configUpdateCmd)
145
+
// Define flags for the config reset command. These are the same flags used by the init command.
146
+
configResetCmd.Flags().StringVar(&configResetInitFlags.ApiToken, "api-token", "", "Optional Codacy API token. If defined, configurations will be fetched from Codacy.")
147
+
configResetCmd.Flags().StringVar(&configResetInitFlags.Provider, "provider", "", "Provider (e.g., gh, bb, gl) to fetch configurations from Codacy. Required when api-token is provided.")
148
+
configResetCmd.Flags().StringVar(&configResetInitFlags.Organization, "organization", "", "Remote organization name to fetch configurations from Codacy. Required when api-token is provided.")
149
+
configResetCmd.Flags().StringVar(&configResetInitFlags.Repository, "repository", "", "Remote repository name to fetch configurations from Codacy. Required when api-token is provided.")
This document outlines the possible scenarios for the `codacy-cli config reset` command, considering the current CLI mode (as defined in `.codacy/cli-config.yaml`) and the flags provided during the command execution.
|**Local**| No flags | Resets configuration to local default settings. Creates/overwrites `.codacy/codacy.yaml` and `tools-configs/` with defaults. `cli-config.yaml` is set to `mode: local`. |
8
+
|**Local**|`--api-token <token>`<br>(missing provider/org/repo) |**Error:** Command exits. Message indicates that `--provider`, `--organization`, and `--repository` are required when `--api-token` is used. |
9
+
|**Local**|`--api-token <token>`<br>`--provider <p>`<br>`--organization <o>`<br>`--repository <r>`| Fetches repository-specific configurations from Codacy API. Creates/overwrites `.codacy/codacy.yaml` and `tools-configs/` based on API response. `cli-config.yaml` is set to `mode: remote`. |
10
+
|**Remote**| No flags |**Error:** Command exits. Message indicates that the CLI is in 'remote' mode, and to reset, API flags (`--api-token`, `--provider`, etc.) must be provided. This prevents accidental reset to local defaults. |
11
+
|**Remote**|`--api-token <token>`<br>(missing provider/org/repo) |**Error:** Command exits. Message indicates that `--provider`, `--organization`, and `--repository` are required when `--api-token` is used. |
12
+
|**Remote**|`--api-token <token>`<br>`--provider <p>`<br>`--organization <o>`<br>`--repository <r>`| Fetches repository-specific configurations from Codacy API. Creates/overwrites `.codacy/codacy.yaml` and `tools-configs/` based on API response. `cli-config.yaml` remains/is set to `mode: remote`. |
13
+
| File missing or unparseable (`.codacy/cli-config.yaml`) | No flags | (Defaults to Local mode) Resets configuration to local default settings. Creates/overwrites `.codacy/codacy.yaml` and `tools-configs/` with defaults. `cli-config.yaml` is set to `mode: local`. **User-facing warning printed to console and logged regarding `cli-config.yaml` issue.**|
14
+
| File missing or unparseable (`.codacy/cli-config.yaml`) |`--api-token <token>`<br>(missing provider/org/repo) | (Defaults to Local mode) **Error:** Command exits. Message indicates that `--provider`, `--organization`, and `--repository` are required when `--api-token` is used. **User-facing warning printed to console and logged regarding `cli-config.yaml` issue.**|
15
+
| File missing or unparseable (`.codacy/cli-config.yaml`) |`--api-token <token>`<br>`--provider <p>`<br>`--organization <o>`<br>`--repository <r>`| (Defaults to Local mode) Fetches repository-specific configurations from Codacy API. Creates/overwrites `.codacy/codacy.yaml` and `tools-configs/` based on API response. `cli-config.yaml` is set to `mode: remote`. **User-facing warning printed to console and logged regarding `cli-config.yaml` issue.**|
16
+
17
+
## Key Points
18
+
19
+
* The `runConfigResetLogic` function determines whether to use local defaults or fetch from the API based purely on the presence of the `ApiToken` flag at the time of its execution.
20
+
* The `cliLocalMode` variable within `runConfigResetLogic` (which influences `CreateConfigurationFiles` and `CliConfigFileTemplate`) is set based on `len(flags.ApiToken) == 0`.
21
+
* If an API token is provided to `config reset`, the resulting `.codacy/cli-config.yaml` will always be set to `mode: remote`.
22
+
* If no token is provided, it will be set to `mode: local`.
23
+
* The validation logic in the `configResetCmd.Run` function occurs *before*`runConfigResetLogic` is called. This validation is responsible for:
24
+
* Informing the user with a console warning if `.codacy/cli-config.yaml` is missing or unparseable, then defaulting to 'local' mode for subsequent checks.
25
+
* Ensuring that if the (potentially defaulted) current CLI mode is "remote", an API token *must* be supplied to proceed with the reset.
26
+
* Ensuring that if an API token *is* supplied, then `--provider`, `--organization`, and `--repository` flags must also be supplied.
27
+
28
+
This table should cover the main operational flows and error conditions for the `config reset` command based on the implemented logic.
0 commit comments