Tool to help you manage your Grafana dashboards using Git.

make_table.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // +build none
  2. package main
  3. import (
  4. "bytes"
  5. "compress/zlib"
  6. "encoding/binary"
  7. "fmt"
  8. "go/format"
  9. "io/ioutil"
  10. "strconv"
  11. "strings"
  12. )
  13. func main() {
  14. data, err := ioutil.ReadFile("table.txt")
  15. if err != nil {
  16. panic(err)
  17. }
  18. var buf bytes.Buffer
  19. for _, line := range strings.Split(string(data), "\n") {
  20. if strings.HasPrefix(line, "/*") || line == "" {
  21. continue
  22. }
  23. sep := strings.IndexByte(line, ':')
  24. if sep == -1 {
  25. panic(line)
  26. }
  27. val, err := strconv.ParseInt(line[:sep], 0, 32)
  28. if err != nil {
  29. panic(err)
  30. }
  31. s, err := strconv.Unquote(line[sep+2:])
  32. if err != nil {
  33. panic(err)
  34. }
  35. if s == "" {
  36. continue
  37. }
  38. if err := binary.Write(&buf, binary.LittleEndian, uint16(val)); err != nil {
  39. panic(err)
  40. }
  41. if err := binary.Write(&buf, binary.LittleEndian, uint8(len(s))); err != nil {
  42. panic(err)
  43. }
  44. buf.WriteString(s)
  45. }
  46. var cbuf bytes.Buffer
  47. w, err := zlib.NewWriterLevel(&cbuf, zlib.BestCompression)
  48. if err != nil {
  49. panic(err)
  50. }
  51. if _, err := w.Write(buf.Bytes()); err != nil {
  52. panic(err)
  53. }
  54. if err := w.Close(); err != nil {
  55. panic(err)
  56. }
  57. buf.Reset()
  58. buf.WriteString("package unidecode\n")
  59. buf.WriteString("// AUTOGENERATED - DO NOT EDIT!\n\n")
  60. fmt.Fprintf(&buf, "var tableData = %q;\n", cbuf.String())
  61. dst, err := format.Source(buf.Bytes())
  62. if err != nil {
  63. panic(err)
  64. }
  65. if err := ioutil.WriteFile("table.go", dst, 0644); err != nil {
  66. panic(err)
  67. }
  68. }