diff options
Diffstat (limited to '')
-rw-r--r-- | pkg/config/config.go | 7 | ||||
-rw-r--r-- | pkg/config/yaml.go | 11 | ||||
-rw-r--r-- | pkg/config/yaml_test.go | 5 |
3 files changed, 12 insertions, 11 deletions
diff --git a/pkg/config/config.go b/pkg/config/config.go index de8e4ad..6d51ffc 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -2,7 +2,6 @@ package config import ( "fmt" - "io/ioutil" "os" "os/user" "runtime" @@ -102,13 +101,13 @@ func Version() string { func Load(path string) (*Config, error) { log.Printf("Reading configuration file '%s'", path) - buf, err := ioutil.ReadFile(path) + f, err := os.Open(path) if err != nil { - return nil, fmt.Errorf("while reading '%s': %w", path, err) + return nil, fmt.Errorf("while opening '%s': %w", path, err) } cfg := WithDefault() - if err = cfg.parse(buf); err != nil { + if err = cfg.parse(f); err != nil { return nil, fmt.Errorf("while parsing: %w", err) } diff --git a/pkg/config/yaml.go b/pkg/config/yaml.go index 93f8c10..9711130 100644 --- a/pkg/config/yaml.go +++ b/pkg/config/yaml.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "io" "reflect" "strings" @@ -62,24 +63,24 @@ func (grpFeed *configGroupFeed) target() string { return grpFeed.Group.Group } -func unmarshal(buf []byte, cfg *Config) (config, error) { +func unmarshal(in io.Reader, cfg *Config) (config, error) { parsedCfg := config{Config: cfg} - if err := yaml.Unmarshal(buf, &parsedCfg); err != nil { + d := yaml.NewDecoder(in) + if err := d.Decode(&parsedCfg); err != nil && err != io.EOF { return config{}, err } - //fmt.Printf("--- parsedCfg:\n%+v\n\n", parsedCfg) return parsedCfg, nil } -func (cfg *Config) parse(buf []byte) error { +func (cfg *Config) parse(in io.Reader) error { var ( err error parsedCfg config ) - if parsedCfg, err = unmarshal(buf, cfg); err != nil { + if parsedCfg, err = unmarshal(in, cfg); err != nil { return fmt.Errorf("while unmarshalling: %w", err) } diff --git a/pkg/config/yaml_test.go b/pkg/config/yaml_test.go index de20c9f..4fc501a 100644 --- a/pkg/config/yaml_test.go +++ b/pkg/config/yaml_test.go @@ -316,8 +316,9 @@ feeds: for _, tt := range tests { tst.Run(tt.name, func(tst *testing.T) { - var buf = []byte(tt.inp) - got, err := unmarshal(buf, WithDefault()) + in := strings.NewReader(tt.inp) + + got, err := unmarshal(in, WithDefault()) if (err != nil) != tt.wantErr { tst.Errorf("parse() error = %v, wantErr %v", err, tt.wantErr) return |