|
| 1 | +package config_file |
| 2 | + |
| 3 | +import ( |
| 4 | + "codacy/cli-v2/config" |
| 5 | + "codacy/cli-v2/plugins" |
| 6 | + "codacy/cli-v2/utils/logger" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | + "gopkg.in/yaml.v3" |
| 12 | +) |
| 13 | + |
| 14 | +// ReadConfigFileWithLogging reads the configuration file and updates the global config with detailed logging |
| 15 | +func ReadConfigFileWithLogging(configPath string) error { |
| 16 | + logger.Debug("Reading configuration file", logrus.Fields{ |
| 17 | + "configPath": configPath, |
| 18 | + }) |
| 19 | + |
| 20 | + // Check if file exists |
| 21 | + if _, err := os.Stat(configPath); os.IsNotExist(err) { |
| 22 | + logger.Error("Configuration file does not exist", logrus.Fields{ |
| 23 | + "configPath": configPath, |
| 24 | + "error": err.Error(), |
| 25 | + }) |
| 26 | + return err |
| 27 | + } |
| 28 | + |
| 29 | + // Read the file |
| 30 | + data, err := os.ReadFile(configPath) |
| 31 | + if err != nil { |
| 32 | + logger.Error("Failed to read configuration file", logrus.Fields{ |
| 33 | + "configPath": configPath, |
| 34 | + "error": err.Error(), |
| 35 | + }) |
| 36 | + return fmt.Errorf("failed to read config file: %w", err) |
| 37 | + } |
| 38 | + |
| 39 | + logger.Debug("Parsing configuration file", logrus.Fields{ |
| 40 | + "configPath": configPath, |
| 41 | + "fileSize": len(data), |
| 42 | + }) |
| 43 | + |
| 44 | + // Parse the YAML |
| 45 | + var configData struct { |
| 46 | + Runtimes []string `yaml:"runtimes"` |
| 47 | + Tools []string `yaml:"tools"` |
| 48 | + } |
| 49 | + |
| 50 | + if err := yaml.Unmarshal(data, &configData); err != nil { |
| 51 | + logger.Error("Failed to parse configuration file", logrus.Fields{ |
| 52 | + "configPath": configPath, |
| 53 | + "error": err.Error(), |
| 54 | + }) |
| 55 | + return fmt.Errorf("failed to parse config file: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + logger.Debug("Configuration parsed successfully", logrus.Fields{ |
| 59 | + "runtimes": configData.Runtimes, |
| 60 | + "tools": configData.Tools, |
| 61 | + }) |
| 62 | + |
| 63 | + // Convert the runtime strings to RuntimeConfig objects |
| 64 | + runtimeConfigs := make([]plugins.RuntimeConfig, 0, len(configData.Runtimes)) |
| 65 | + for _, rt := range configData.Runtimes { |
| 66 | + ct, err := parseConfigTool(rt) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + runtimeConfigs = append(runtimeConfigs, plugins.RuntimeConfig{ |
| 71 | + Name: ct.name, |
| 72 | + Version: ct.version, |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + // Add all runtimes at once |
| 77 | + if err := config.Config.AddRuntimes(runtimeConfigs); err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + // Convert the tool strings to ToolConfig objects |
| 82 | + toolConfigs := make([]plugins.ToolConfig, 0, len(configData.Tools)) |
| 83 | + for _, tl := range configData.Tools { |
| 84 | + ct, err := parseConfigTool(tl) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + toolConfigs = append(toolConfigs, plugins.ToolConfig{ |
| 89 | + Name: ct.name, |
| 90 | + Version: ct.version, |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + // Add all tools at once |
| 95 | + if err := config.Config.AddTools(toolConfigs); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
0 commit comments