30 lines
513 B
Go
30 lines
513 B
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type YamlConfigLoader struct {
|
|
FilePath string
|
|
}
|
|
|
|
func (l *YamlConfigLoader) LoadConfig() (*Config, error) {
|
|
data, err := os.ReadFile(l.FilePath)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return DefaultConfig(), nil
|
|
}
|
|
return nil, fmt.Errorf("reading config file: %w", err)
|
|
}
|
|
|
|
var cfg Config
|
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
|
return nil, fmt.Errorf("parsing config file: %w", err)
|
|
}
|
|
|
|
return cfg.applyDefaults(), nil
|
|
}
|