map.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * The ephemeralvariablemap is used "only" for variable substitution in config
  3. * titles, shell arguments, etc, in the foorm of {{ key }}, like Jinja2.
  4. *
  5. * OliveTin itself really only ever "writes" to this map, mostly by loading
  6. * EntityFiles, and the only form of "reading" is for the variable substitution
  7. * in configs.
  8. */
  9. package stringvariables
  10. import (
  11. "github.com/prometheus/client_golang/prometheus"
  12. "github.com/prometheus/client_golang/prometheus/promauto"
  13. "strings"
  14. "sync"
  15. )
  16. var (
  17. contents map[string]string
  18. metricSvCount = promauto.NewGauge(prometheus.GaugeOpts{
  19. Name: "olivetin_sv_count",
  20. Help: "The number entries in the sv map",
  21. })
  22. rwmutex = sync.RWMutex{}
  23. )
  24. func init() {
  25. rwmutex.Lock()
  26. contents = make(map[string]string)
  27. rwmutex.Unlock()
  28. }
  29. func Get(key string) string {
  30. rwmutex.RLock()
  31. v, ok := contents[key]
  32. rwmutex.RUnlock()
  33. if !ok {
  34. return ""
  35. } else {
  36. return v
  37. }
  38. }
  39. func GetAll() map[string]string {
  40. return contents
  41. }
  42. func Set(key string, value string) {
  43. rwmutex.Lock()
  44. contents[key] = value
  45. metricSvCount.Set(float64(len(contents)))
  46. rwmutex.Unlock()
  47. }
  48. func RemoveKeysThatStartWith(search string) {
  49. rwmutex.Lock()
  50. for k := range contents {
  51. if strings.HasPrefix(k, search) {
  52. delete(contents, k)
  53. }
  54. }
  55. rwmutex.Unlock()
  56. }