Tool to help you manage your Grafana dashboards using Git.

versions.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "time"
  8. gogit "gopkg.in/src-d/go-git.v4"
  9. "gopkg.in/src-d/go-git.v4/plumbing/object"
  10. )
  11. // getDashboardsVersions reads the "versions.json" file at the root of the git
  12. // repository and returns its content as a map.
  13. // If the file doesn't exist, returns an empty map.
  14. // Return an error if there was an issue looking for the file (except when the
  15. // file doesn't exist), reading it or formatting its content into a map.
  16. func getDashboardsVersions(clonePath string) (versions map[string]int, err error) {
  17. versions = make(map[string]int)
  18. filename := clonePath + "/versions.json"
  19. _, err = os.Stat(filename)
  20. if os.IsNotExist(err) {
  21. return versions, nil
  22. }
  23. data, err := ioutil.ReadFile(filename)
  24. if err != nil {
  25. return
  26. }
  27. err = json.Unmarshal(data, &versions)
  28. return
  29. }
  30. // writeVersions updates or creates the "versions.json" file at the root of the
  31. // git repository. It takes as parameter a map of versions computed by
  32. // getDashboardsVersions and a map linking a dashboard slug to an instance of
  33. // diffVersion instance, and uses them both to compute an updated map of
  34. // versions that it will convert to JSON, indent and write down into the
  35. // "versions.json" file.
  36. // Returns an error if there was an issue when conerting to JSON, indenting or
  37. // writing on disk.
  38. func writeVersions(
  39. versions map[string]int, dv map[string]diffVersion, clonePath string,
  40. ) (err error) {
  41. for slug, diff := range dv {
  42. versions[slug] = diff.newVersion
  43. }
  44. rawJSON, err := json.Marshal(versions)
  45. if err != nil {
  46. return
  47. }
  48. indentedJSON, err := indent(rawJSON)
  49. if err != nil {
  50. return
  51. }
  52. filename := clonePath + "/versions.json"
  53. return rewriteFile(filename, indentedJSON)
  54. }
  55. // commitNewVersions creates a git commit from updated dashboard files (that
  56. // have previously been added to the git index) and an updated "versions.json"
  57. // file that it creates (with writeVersions) and add to the index.
  58. // Returns an error if there was an issue when creating the "versions.json"
  59. // file, adding it to the index or creating the commit.
  60. func commitNewVersions(
  61. versions map[string]int, dv map[string]diffVersion, worktree *gogit.Worktree,
  62. clonePath string,
  63. ) (err error) {
  64. if err = writeVersions(versions, dv, clonePath); err != nil {
  65. return err
  66. }
  67. if _, err = worktree.Add("versions.json"); err != nil {
  68. return err
  69. }
  70. _, err = worktree.Commit(getCommitMessage(dv), &gogit.CommitOptions{
  71. Author: &object.Signature{
  72. Name: "Grafana Dashboard Manager",
  73. Email: "grafana@cozycloud.cc",
  74. When: time.Now(),
  75. },
  76. })
  77. return
  78. }
  79. // getCommitMessage creates a commit message that summarises the version updates
  80. // included in the commit.
  81. func getCommitMessage(dv map[string]diffVersion) string {
  82. message := "Updated dashboards\n"
  83. for slug, diff := range dv {
  84. message += fmt.Sprintf(
  85. "%s: %d => %d\n", slug, diff.oldVersion, diff.newVersion,
  86. )
  87. }
  88. return message
  89. }