From 45bfe0a9b247b4ec9b00ea90e56437159e5adcf3 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Thu, 15 Jun 2023 05:40:37 +0000 Subject: [PATCH] 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 --- internal/pkg/config/config.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 4a43a46..056d2f1 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -62,14 +62,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)