LoadConfig loads configuration from file
()
| 114 | |
| 115 | // LoadConfig loads configuration from file |
| 116 | func (m *Manager) LoadConfig() error { |
| 117 | // Check if config file exists |
| 118 | if _, err := os.Stat(m.configFile); errors.Is(err, fs.ErrNotExist) { |
| 119 | // Use defaults if config file doesn't exist |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | viper.SetConfigFile(m.configFile) |
| 124 | viper.SetConfigType("yaml") |
| 125 | |
| 126 | if err := viper.ReadInConfig(); err != nil { |
| 127 | return fmt.Errorf("error reading config file: %w", err) |
| 128 | } |
| 129 | |
| 130 | if err := viper.Unmarshal(m.config); err != nil { |
| 131 | return fmt.Errorf("error unmarshaling config: %w", err) |
| 132 | } |
| 133 | |
| 134 | // Handle backward compatibility: set defaults for fields that may not exist in older configs |
| 135 | // If UpdateInterval is 0 or not set, use default of 60 minutes |
| 136 | if m.config.UpdateInterval <= 0 { |
| 137 | m.config.UpdateInterval = 60 |
| 138 | } |
| 139 | |
| 140 | // If Integrations map is nil (not set in old configs), initialize it |
| 141 | if m.config.Integrations == nil { |
| 142 | m.config.Integrations = make(map[string]interface{}) |
| 143 | } |
| 144 | |
| 145 | // Ensure all available integrations are present in the map with default value |
| 146 | // This ensures config.yml always shows all integrations, even if they're disabled |
| 147 | for _, integrationName := range AvailableIntegrations { |
| 148 | if _, exists := m.config.Integrations[integrationName]; !exists { |
| 149 | if integrationName == "compliance" { |
| 150 | // Default compliance to "on-demand" mode |
| 151 | m.config.Integrations[integrationName] = "on-demand" |
| 152 | } else { |
| 153 | m.config.Integrations[integrationName] = false |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Validate and normalize compliance value |
| 159 | if complianceVal, exists := m.config.Integrations["compliance"]; exists { |
| 160 | switch v := complianceVal.(type) { |
| 161 | case bool: |
| 162 | // Keep bool as-is (false = disabled, true = enabled with auto-scans) |
| 163 | case string: |
| 164 | // Normalize string values |
| 165 | switch v { |
| 166 | case "on-demand", "on_demand": |
| 167 | m.config.Integrations["compliance"] = "on-demand" |
| 168 | case "true": |
| 169 | m.config.Integrations["compliance"] = true |
| 170 | case "false": |
| 171 | m.config.Integrations["compliance"] = false |
| 172 | } |
| 173 | } |
no test coverage detected