Tool to help you manage your Grafana dashboards using Git.

config.go 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package config
  2. import (
  3. "io/ioutil"
  4. "strings"
  5. "gopkg.in/yaml.v2"
  6. )
  7. // Config is the Go representation of the configuration file. It is filled when
  8. // parsing the said file.
  9. type Config struct {
  10. Grafana GrafanaSettings `yaml:"grafana"`
  11. Git GitSettings `yaml:"git"`
  12. Webhook WebhookSettings `yaml:"webhook"`
  13. }
  14. // GrafanaSettings contains the data required to talk to the Grafana HTTP API.
  15. type GrafanaSettings struct {
  16. BaseURL string `yaml:"base_url"`
  17. APIKey string `yaml:"api_key"`
  18. IgnorePrefix string `yaml:"ignore_prefix,omitempty"`
  19. }
  20. // GitSettings contains the data required to interact with the Git repository.
  21. type GitSettings struct {
  22. URL string `yaml:"url"`
  23. User string `yaml:"user"`
  24. PrivateKeyPath string `yaml:"private_key"`
  25. ClonePath string `yaml:"clone_path"`
  26. CommitsAuthor CommitsAuthorConfig `yaml:"commits_author"`
  27. }
  28. // CommitsAuthorConfig contains the configuration (name + email address) to use
  29. // when commiting to Git.
  30. type CommitsAuthorConfig struct {
  31. Name string `yaml:"name"`
  32. Email string `yaml:"email"`
  33. }
  34. // WebhookSettings contains the data required to setup the GitLab webhook.
  35. // We declare the port as a string because, although it's a number, it's only
  36. // used in a string concatenation when creating the webhook.
  37. type WebhookSettings struct {
  38. Interface string `yaml:"interface"`
  39. Port string `yaml:"port"`
  40. Path string `yaml:"path"`
  41. Secret string `yaml:"secret"`
  42. }
  43. // Load opens a given configuration file and parses it into an instance of the
  44. // Config structure.
  45. // Returns an error if there was an issue whith reading or parsing the file.
  46. func Load(filename string) (cfg *Config, err error) {
  47. rawCfg, err := ioutil.ReadFile(filename)
  48. if err != nil {
  49. return
  50. }
  51. cfg = new(Config)
  52. err = yaml.Unmarshal(rawCfg, cfg)
  53. cfg.Grafana.IgnorePrefix = strings.ToLower(cfg.Grafana.IgnorePrefix)
  54. return
  55. }