util.go 569 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package core
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "strings"
  7. "github.com/go-yaml/yaml"
  8. )
  9. // Some utils
  10. func ParseConfig(srcfile string, out interface{}) (err error) {
  11. if srcfile == "" {
  12. return
  13. }
  14. f, err := os.Open(srcfile)
  15. if err != nil {
  16. return err
  17. }
  18. defer f.Close()
  19. if strings.HasSuffix(srcfile, ".json") {
  20. // Read as JSON
  21. json.NewDecoder(f).Decode(out)
  22. return
  23. }
  24. if strings.HasSuffix(srcfile, ".yaml") {
  25. data, err := ioutil.ReadAll(f)
  26. if err != nil {
  27. return err
  28. }
  29. // Read as yaml
  30. yaml.Unmarshal(data, out)
  31. }
  32. return err
  33. }