confload.go 756 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package megauploader
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "github.com/pkg/errors"
  6. yaml "gopkg.in/yaml.v2"
  7. )
  8. // ParseConfFile will read and load a config file
  9. func ParseConfFile(filename string) (Config, error) {
  10. buf, err := os.Open(filename)
  11. if err != nil {
  12. return Config{}, errors.Wrap(err, "Error opening config file")
  13. }
  14. defer buf.Close()
  15. content, err := ioutil.ReadAll(buf)
  16. if err != nil {
  17. return Config{}, errors.Wrap(err, "Error reading config file")
  18. }
  19. return ParseConf(content)
  20. }
  21. // ParseConf will parse configuration content
  22. func ParseConf(content []byte) (Config, error) {
  23. var c Config
  24. err := yaml.UnmarshalStrict(content, &c)
  25. if err != nil {
  26. return c, err
  27. }
  28. if c.UserHome == nil {
  29. c.UserHome = new(UserHome)
  30. }
  31. return c, nil
  32. }