|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + |
| 8 | + "codacy/cli-v2/cmd/configsetup" |
| 9 | + "codacy/cli-v2/config" |
| 10 | + "codacy/cli-v2/domain" |
| 11 | + "codacy/cli-v2/utils" |
| 12 | + |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +// configUpdateInitFlags holds the flags for the config update command, similar to init command flags. |
| 17 | +var configUpdateInitFlags domain.InitFlags |
| 18 | + |
| 19 | +var configCmd = &cobra.Command{ |
| 20 | + Use: "config", |
| 21 | + Short: "Manage Codacy configuration", |
| 22 | +} |
| 23 | + |
| 24 | +var configUpdateCmd = &cobra.Command{ |
| 25 | + Use: "update", |
| 26 | + Short: "Update Codacy configuration, or initialize if it doesn't exist", |
| 27 | + Long: "Updates the Codacy configuration files. If .codacy/codacy.yaml does not exist, it runs the initialization process.", |
| 28 | + Run: func(cmd *cobra.Command, args []string) { |
| 29 | + codacyConfigFile := config.Config.ProjectConfigFile() |
| 30 | + // Check if the main configuration file exists |
| 31 | + if _, err := os.Stat(codacyConfigFile); os.IsNotExist(err) { |
| 32 | + fmt.Println("Configuration file (.codacy/codacy.yaml) not found, running initialization logic...") |
| 33 | + runConfigUpdateLogic(cmd, args, configUpdateInitFlags) |
| 34 | + } else { |
| 35 | + fmt.Println("Updating existing Codacy configuration...") |
| 36 | + runConfigUpdateLogic(cmd, args, configUpdateInitFlags) |
| 37 | + } |
| 38 | + }, |
| 39 | +} |
| 40 | + |
| 41 | +// runConfigUpdateLogic contains the core logic for updating or initializing the configuration. |
| 42 | +// It mirrors the behavior of the original init command but uses shared functions from the configsetup package. |
| 43 | +func runConfigUpdateLogic(cmd *cobra.Command, args []string, flags domain.InitFlags) { |
| 44 | + // Create local .codacy directory first |
| 45 | + if err := config.Config.CreateLocalCodacyDir(); err != nil { |
| 46 | + log.Fatalf("Failed to create local codacy directory: %v", err) |
| 47 | + } |
| 48 | + |
| 49 | + // Create .codacy/tools-configs directory |
| 50 | + toolsConfigDir := config.Config.ToolsConfigDirectory() |
| 51 | + if err := os.MkdirAll(toolsConfigDir, utils.DefaultDirPerms); err != nil { |
| 52 | + log.Fatalf("Failed to create tools-configs directory: %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + // Determine if running in local mode (no API token) |
| 56 | + cliLocalMode := len(flags.ApiToken) == 0 |
| 57 | + |
| 58 | + if cliLocalMode { |
| 59 | + fmt.Println() |
| 60 | + fmt.Println("ℹ️ No API token was specified. Proceeding with local default configurations.") |
| 61 | + noTools := []domain.Tool{} // Empty slice for tools as we are in local mode without specific toolset from API initially |
| 62 | + if err := configsetup.CreateConfigurationFiles(noTools, cliLocalMode); err != nil { |
| 63 | + log.Fatalf("Failed to create base configuration files: %v", err) |
| 64 | + } |
| 65 | + // Create default configuration files for tools |
| 66 | + if err := configsetup.BuildDefaultConfigurationFiles(toolsConfigDir, flags); err != nil { |
| 67 | + log.Fatalf("Failed to build default tool configuration files: %v", err) |
| 68 | + } |
| 69 | + // Create the languages configuration file for local mode |
| 70 | + if err := configsetup.CreateLanguagesConfigFileLocal(toolsConfigDir); err != nil { |
| 71 | + log.Fatalf("Failed to create local languages configuration file: %v", err) |
| 72 | + } |
| 73 | + } else { |
| 74 | + // API token provided, fetch configuration from Codacy |
| 75 | + fmt.Println("API token specified. Fetching repository-specific configurations from Codacy...") |
| 76 | + if err := configsetup.BuildRepositoryConfigurationFiles(flags); err != nil { |
| 77 | + log.Fatalf("Failed to build repository-specific configuration files: %v", err) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Create or update .gitignore file in .codacy directory |
| 82 | + if err := configsetup.CreateGitIgnoreFile(); err != nil { |
| 83 | + log.Printf("Warning: Failed to create or update .codacy/.gitignore: %v", err) // Log as warning, not fatal |
| 84 | + } |
| 85 | + |
| 86 | + fmt.Println() |
| 87 | + fmt.Println("✅ Successfully initialized/updated Codacy configuration!") |
| 88 | + fmt.Println() |
| 89 | + fmt.Println("🔧 Next steps:") |
| 90 | + fmt.Println(" 1. Run 'codacy-cli install' to install all dependencies based on the new/updated configuration.") |
| 91 | + fmt.Println(" 2. Run 'codacy-cli analyze' to start analyzing your code.") |
| 92 | + fmt.Println() |
| 93 | +} |
| 94 | + |
| 95 | +func init() { |
| 96 | + // 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) |
| 104 | + // Add the config command to the root command |
| 105 | + rootCmd.AddCommand(configCmd) |
| 106 | +} |
0 commit comments