|
7 | 7 | "regexp" |
8 | 8 | "strings" |
9 | 9 |
|
| 10 | + "github.com/mistweaverco/kuba/internal/lib/log" |
10 | 11 | "gopkg.in/yaml.v3" |
11 | 12 | ) |
12 | 13 |
|
@@ -151,54 +152,90 @@ func processValueInterpolations(config *KubaConfig) error { |
151 | 152 |
|
152 | 153 | // LoadKubaConfig loads the kuba.yaml configuration file |
153 | 154 | func LoadKubaConfig(configPath string) (*KubaConfig, error) { |
| 155 | + logger := log.NewLogger() |
| 156 | + |
154 | 157 | if configPath == "" { |
155 | 158 | configPath = "kuba.yaml" |
156 | 159 | } |
157 | 160 |
|
| 161 | + logger.Debug("Loading configuration file", "path", configPath) |
| 162 | + |
158 | 163 | // Check if file exists |
159 | 164 | if _, err := os.Stat(configPath); os.IsNotExist(err) { |
| 165 | + logger.Debug("Configuration file not found", "path", configPath) |
160 | 166 | return nil, fmt.Errorf("configuration file not found: %s", configPath) |
161 | 167 | } |
162 | 168 |
|
163 | 169 | // Read file |
| 170 | + logger.Debug("Reading configuration file") |
164 | 171 | data, err := os.ReadFile(configPath) |
165 | 172 | if err != nil { |
| 173 | + logger.Debug("Failed to read configuration file", "path", configPath, "error", err) |
166 | 174 | return nil, fmt.Errorf("failed to read configuration file: %w", err) |
167 | 175 | } |
168 | 176 |
|
| 177 | + logger.Debug("Configuration file read successfully", "size_bytes", len(data)) |
| 178 | + |
169 | 179 | // Parse YAML |
| 180 | + logger.Debug("Parsing YAML configuration") |
170 | 181 | var config KubaConfig |
171 | 182 | if err := yaml.Unmarshal(data, &config); err != nil { |
| 183 | + logger.Debug("Failed to parse YAML configuration", "error", err) |
172 | 184 | return nil, fmt.Errorf("failed to parse configuration file: %w", err) |
173 | 185 | } |
174 | 186 |
|
| 187 | + logger.Debug("YAML parsed successfully", "environments_count", len(config.Environments)) |
| 188 | + |
175 | 189 | // Process environment variable interpolations |
| 190 | + logger.Debug("Processing environment variable interpolations") |
176 | 191 | if err := processValueInterpolations(&config); err != nil { |
| 192 | + logger.Debug("Failed to process environment variable interpolations", "error", err) |
177 | 193 | return nil, fmt.Errorf("failed to process environment variable interpolations: %w", err) |
178 | 194 | } |
179 | 195 |
|
| 196 | + logger.Debug("Environment variable interpolations processed successfully") |
| 197 | + |
180 | 198 | // Validate configuration |
| 199 | + logger.Debug("Validating configuration") |
181 | 200 | if err := validateConfig(&config); err != nil { |
| 201 | + logger.Debug("Configuration validation failed", "error", err) |
182 | 202 | return nil, fmt.Errorf("invalid configuration: %w", err) |
183 | 203 | } |
184 | 204 |
|
| 205 | + logger.Debug("Configuration validation passed") |
185 | 206 | return &config, nil |
186 | 207 | } |
187 | 208 |
|
188 | 209 | // GetEnvironment returns the configuration for a specific environment |
189 | 210 | func (c *KubaConfig) GetEnvironment(envName string) (*Environment, error) { |
| 211 | + logger := log.NewLogger() |
| 212 | + |
190 | 213 | if envName == "" { |
191 | 214 | envName = "default" |
| 215 | + logger.Debug("No environment specified, using default") |
192 | 216 | } |
193 | 217 |
|
| 218 | + logger.Debug("Getting environment configuration", "requested_env", envName, "available_environments", len(c.Environments)) |
| 219 | + |
194 | 220 | env, exists := c.Environments[envName] |
195 | 221 | if !exists { |
| 222 | + logger.Debug("Environment not found in configuration", "requested_env", envName, "available_environments", getEnvironmentNames(c.Environments)) |
196 | 223 | return nil, fmt.Errorf("environment '%s' not found in configuration", envName) |
197 | 224 | } |
198 | 225 |
|
| 226 | + logger.Debug("Environment configuration retrieved", "environment", envName, "provider", env.Provider, "project", env.Project, "mappings_count", len(env.Mappings)) |
199 | 227 | return &env, nil |
200 | 228 | } |
201 | 229 |
|
| 230 | +// getEnvironmentNames returns a slice of available environment names |
| 231 | +func getEnvironmentNames(environments map[string]Environment) []string { |
| 232 | + names := make([]string, 0, len(environments)) |
| 233 | + for name := range environments { |
| 234 | + names = append(names, name) |
| 235 | + } |
| 236 | + return names |
| 237 | +} |
| 238 | + |
202 | 239 | // validateConfig validates the configuration structure |
203 | 240 | func validateConfig(config *KubaConfig) error { |
204 | 241 | if len(config.Environments) == 0 { |
|
0 commit comments