fix: handle with empty config file

This commit is contained in:
Jason Song 2023-06-15 12:14:54 +08:00
parent 946c41cf4f
commit c356636b07
No known key found for this signature in database
GPG key ID: 8402EEEE4511A8B5

View file

@ -61,14 +61,12 @@ type Config struct {
func LoadDefault(file string) (*Config, error) {
cfg := &Config{}
if file != "" {
f, err := os.Open(file)
content, err := os.ReadFile(file)
if err != nil {
return nil, err
return nil, fmt.Errorf("open config file %q: %w", file, err)
}
defer f.Close()
decoder := yaml.NewDecoder(f)
if err := decoder.Decode(&cfg); err != nil {
return nil, err
if err := yaml.Unmarshal(content, cfg); err != nil {
return nil, fmt.Errorf("parse config file %q: %w", file, err)
}
}
compatibleWithOldEnvs(file != "", cfg)