config_reader.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package dashboards
  2. import (
  3. "io/ioutil"
  4. "path/filepath"
  5. "strings"
  6. "github.com/grafana/grafana/pkg/log"
  7. yaml "gopkg.in/yaml.v2"
  8. )
  9. type configReader struct {
  10. path string
  11. log log.Logger
  12. }
  13. func (cr *configReader) readConfig() ([]*DashboardsAsConfig, error) {
  14. var dashboards []*DashboardsAsConfig
  15. files, err := ioutil.ReadDir(cr.path)
  16. if err != nil {
  17. cr.log.Error("cant read dashboard provisioning files from directory", "path", cr.path)
  18. return dashboards, nil
  19. }
  20. for _, file := range files {
  21. if !strings.HasSuffix(file.Name(), ".yaml") && !strings.HasSuffix(file.Name(), ".yml") {
  22. continue
  23. }
  24. filename, _ := filepath.Abs(filepath.Join(cr.path, file.Name()))
  25. yamlFile, err := ioutil.ReadFile(filename)
  26. if err != nil {
  27. return nil, err
  28. }
  29. var dashCfg []*DashboardsAsConfig
  30. err = yaml.Unmarshal(yamlFile, &dashCfg)
  31. if err != nil {
  32. return nil, err
  33. }
  34. dashboards = append(dashboards, dashCfg...)
  35. }
  36. for i := range dashboards {
  37. if dashboards[i].OrgId == 0 {
  38. dashboards[i].OrgId = 1
  39. }
  40. }
  41. return dashboards, nil
  42. }