Other(refactor): Move Settings out of frontend

This commit is contained in:
James Houlahan
2022-08-22 12:32:55 +02:00
committed by Jakub
parent 310c6a1ccf
commit 0c7453684b
17 changed files with 178 additions and 129 deletions

View File

@ -30,9 +30,9 @@ import (
)
type keyValueStore struct {
cache map[string]string
path string
lock *sync.RWMutex
vals map[Key]string
path string
lock *sync.RWMutex
}
// newKeyValueStore returns loaded preferences.
@ -48,14 +48,14 @@ func newKeyValueStore(path string) *keyValueStore {
}
func (p *keyValueStore) load() error {
if p.cache != nil {
if p.vals != nil {
return nil
}
p.lock.Lock()
defer p.lock.Unlock()
p.cache = map[string]string{}
p.vals = make(map[Key]string)
f, err := os.Open(p.path)
if err != nil {
@ -63,18 +63,18 @@ func (p *keyValueStore) load() error {
}
defer f.Close() //nolint:errcheck,gosec
return json.NewDecoder(f).Decode(&p.cache)
return json.NewDecoder(f).Decode(&p.vals)
}
func (p *keyValueStore) save() error {
if p.cache == nil {
if p.vals == nil {
return errors.New("cannot save preferences: cache is nil")
}
p.lock.Lock()
defer p.lock.Unlock()
b, err := json.MarshalIndent(p.cache, "", "\t")
b, err := json.MarshalIndent(p.vals, "", "\t")
if err != nil {
return err
}
@ -82,24 +82,24 @@ func (p *keyValueStore) save() error {
return ioutil.WriteFile(p.path, b, 0o600)
}
func (p *keyValueStore) setDefault(key, value string) {
func (p *keyValueStore) setDefault(key Key, value string) {
if p.Get(key) == "" {
p.Set(key, value)
}
}
func (p *keyValueStore) Get(key string) string {
func (p *keyValueStore) Get(key Key) string {
p.lock.RLock()
defer p.lock.RUnlock()
return p.cache[key]
return p.vals[key]
}
func (p *keyValueStore) GetBool(key string) bool {
func (p *keyValueStore) GetBool(key Key) bool {
return p.Get(key) == "true"
}
func (p *keyValueStore) GetInt(key string) int {
func (p *keyValueStore) GetInt(key Key) int {
if p.Get(key) == "" {
return 0
}
@ -112,7 +112,7 @@ func (p *keyValueStore) GetInt(key string) int {
return value
}
func (p *keyValueStore) GetFloat64(key string) float64 {
func (p *keyValueStore) GetFloat64(key Key) float64 {
if p.Get(key) == "" {
return 0
}
@ -125,9 +125,9 @@ func (p *keyValueStore) GetFloat64(key string) float64 {
return value
}
func (p *keyValueStore) Set(key, value string) {
func (p *keyValueStore) Set(key Key, value string) {
p.lock.Lock()
p.cache[key] = value
p.vals[key] = value
p.lock.Unlock()
if err := p.save(); err != nil {
@ -135,7 +135,7 @@ func (p *keyValueStore) Set(key, value string) {
}
}
func (p *keyValueStore) SetBool(key string, value bool) {
func (p *keyValueStore) SetBool(key Key, value bool) {
if value {
p.Set(key, "true")
} else {
@ -143,10 +143,10 @@ func (p *keyValueStore) SetBool(key string, value bool) {
}
}
func (p *keyValueStore) SetInt(key string, value int) {
func (p *keyValueStore) SetInt(key Key, value int) {
p.Set(key, strconv.Itoa(value))
}
func (p *keyValueStore) SetFloat64(key string, value float64) {
func (p *keyValueStore) SetFloat64(key Key, value float64) {
p.Set(key, fmt.Sprintf("%v", value))
}