diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-05-01 17:05:07 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-05-01 17:05:07 +0200 |
commit | f944124325dd785085fec59210306111b3eab3b7 (patch) | |
tree | 53f88d24f42093df907f68a1a7eaf942bed0aeee /pkg/config | |
parent | d7de88398ca67d7213fb849db60e5963fd3bc32f (diff) | |
download | feed2imap-go-f944124325dd785085fec59210306111b3eab3b7.tar.gz feed2imap-go-f944124325dd785085fec59210306111b3eab3b7.tar.bz2 feed2imap-go-f944124325dd785085fec59210306111b3eab3b7.zip |
Yaml: Pass `Reader` around instead of []byte
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 |