Accept empty file as config (#241)

Close #240

`yaml.Decoder.Decode` will return EOF when the root node is nil , see https://github.com/go-yaml/yaml/blob/v3/yaml.go#L125

While `yaml.Unmarshal` will accept it, see https://github.com/go-yaml/yaml/blob/v3/yaml.go#L162

Reviewed-on: https://gitea.com/gitea/act_runner/pulls/241
Reviewed-by: Zettat123 <zettat123@noreply.gitea.com>
pull/41/head
Jason Song 2023-06-15 05:40:37 +00:00
parent 316534996a
commit 45bfe0a9b2
1 changed files with 4 additions and 6 deletions

View File

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