Skip to content

Commit

Permalink
feat: add config.Read method to unmarshal config from io.Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
zbindenren committed Dec 31, 2020
1 parent b874c81 commit aecbc18
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package config
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -98,21 +99,36 @@ func Load(dir string) (*Changelog, error) {
return nil, ErrNotFound
}

b, err := ioutil.ReadFile(configPath) // nolint: gosec
r, err := os.Open(configPath) // nolint: gosec
if err != nil {
return nil, fmt.Errorf("failed to read %s: %w", configPath, err)
return nil, fmt.Errorf("open to read %s: %w", configPath, err)
}

c, err := Read(r)
if err != nil {
return nil, fmt.Errorf("failed to load config from %s: %w", configPath, err)
}

if len(c.Sections) == 0 {
return nil, ErrEmpty
}

return c, nil
}

// Read unmarshals config.Changelog from a io.Reader.
func Read(r io.Reader) (*Changelog, error) {
b, err := ioutil.ReadAll(r) // nolint: gosec
if err != nil {
return nil, err
}

c := Changelog{
Flavor: "gitlab",
}

if err := yaml.Unmarshal(b, &c); err != nil {
return nil, fmt.Errorf("failed to parse %s: %w", configPath, err)
}

if len(c.Sections) == 0 {
return nil, ErrEmpty
return nil, fmt.Errorf("failed to unmarshal yaml: %w", err)
}

return &c, nil
Expand Down

0 comments on commit aecbc18

Please sign in to comment.