Files
OliveTin/service/internal/stringvariables/map.go
James Read ff31abe66c
Some checks failed
Build Snapshot / build-snapshot (push) Waiting to run
DevSkim / DevSkim (push) Waiting to run
Buf CI / buf (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Codestyle checks / codestyle (push) Has been cancelled
refactor: Project directories (#541)
2025-03-22 01:06:59 +00:00

77 lines
1.3 KiB
Go

/**
* The ephemeralvariablemap is used "only" for variable substitution in config
* titles, shell arguments, etc, in the foorm of {{ key }}, like Jinja2.
*
* OliveTin itself really only ever "writes" to this map, mostly by loading
* EntityFiles, and the only form of "reading" is for the variable substitution
* in configs.
*/
package stringvariables
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"strings"
"sync"
)
var (
contents map[string]string
metricSvCount = promauto.NewGauge(prometheus.GaugeOpts{
Name: "olivetin_sv_count",
Help: "The number entries in the sv map",
})
rwmutex = sync.RWMutex{}
)
func init() {
rwmutex.Lock()
contents = make(map[string]string)
rwmutex.Unlock()
}
func Get(key string) string {
rwmutex.RLock()
v, ok := contents[key]
rwmutex.RUnlock()
if !ok {
return ""
} else {
return v
}
}
func GetAll() map[string]string {
return contents
}
func Set(key string, value string) {
rwmutex.Lock()
contents[key] = value
metricSvCount.Set(float64(len(contents)))
rwmutex.Unlock()
}
func RemoveKeysThatStartWith(search string) {
rwmutex.Lock()
for k, _ := range contents {
if strings.HasPrefix(k, search) {
delete(contents, k)
}
}
rwmutex.Unlock()
}