loadConfigFile reads and parses a config file. Returns the parsed config and a set of keys that were present in the file. The keys set is used to determine which fields to merge (even if zero-valued).
(configFile string)
| 173 | // Returns the parsed config and a set of keys that were present in the file. |
| 174 | // The keys set is used to determine which fields to merge (even if zero-valued). |
| 175 | func loadConfigFile(configFile string) (*config.Config, map[string]struct{}, error) { |
| 176 | data, err := os.ReadFile(configFile) |
| 177 | if err != nil { |
| 178 | return nil, nil, fmt.Errorf("failed to read config file: %w", err) |
| 179 | } |
| 180 | |
| 181 | // First pass: get the set of keys present in the file |
| 182 | var rawMap map[string]any |
| 183 | if err := yaml.Unmarshal(data, &rawMap); err != nil { |
| 184 | return nil, nil, fmt.Errorf("failed to parse config file: %w", err) |
| 185 | } |
| 186 | |
| 187 | keys := make(map[string]struct{}) |
| 188 | for k := range rawMap { |
| 189 | keys[k] = struct{}{} |
| 190 | } |
| 191 | |
| 192 | // Second pass: unmarshal into the config struct |
| 193 | cfg := &config.Config{} |
| 194 | if err := unmarshalConfigStrict(data, cfg); err != nil { |
| 195 | return nil, nil, fmt.Errorf("failed to parse config file: %w", err) |
| 196 | } |
| 197 | |
| 198 | return cfg, keys, nil |
| 199 | } |
| 200 | |
| 201 | // unmarshalConfigStrict unmarshals YAML config data with support for time.Duration fields. |
| 202 | // It returns an error if the config contains unknown fields, ensuring the user is aware |
no test coverage detected