2021-03-10 15:40:34 +00:00
|
|
|
package config
|
|
|
|
|
2021-05-03 21:01:06 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"sync"
|
|
|
|
)
|
2021-03-10 15:40:34 +00:00
|
|
|
|
2021-05-03 21:01:06 +00:00
|
|
|
// defaultStorage implements config.Storage, providing in-memory config.
|
|
|
|
// Indexed by section, then key.
|
|
|
|
type defaultStorage struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
sections map[string]map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDefaultStorage() *defaultStorage {
|
|
|
|
return &defaultStorage{
|
|
|
|
sections: map[string]map[string]string{},
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 15:40:34 +00:00
|
|
|
|
2021-05-03 21:01:06 +00:00
|
|
|
// GetSectionList returns a slice of strings with names for all the sections.
|
|
|
|
func (s *defaultStorage) GetSectionList() []string {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
sections := make([]string, 0, len(s.sections))
|
|
|
|
for section := range s.sections {
|
|
|
|
sections = append(sections, section)
|
|
|
|
}
|
|
|
|
return sections
|
2021-03-10 15:40:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 21:01:06 +00:00
|
|
|
// HasSection returns true if section exists in the config.
|
|
|
|
func (s *defaultStorage) HasSection(section string) bool {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
_, hasSection := s.sections[section]
|
|
|
|
return hasSection
|
2021-03-10 15:40:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 21:01:06 +00:00
|
|
|
// DeleteSection deletes the specified section.
|
|
|
|
func (s *defaultStorage) DeleteSection(section string) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
delete(s.sections, section)
|
2021-03-10 15:40:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 21:01:06 +00:00
|
|
|
// GetKeyList returns the keys in this section.
|
|
|
|
func (s *defaultStorage) GetKeyList(section string) []string {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
theSection := s.sections[section]
|
|
|
|
keys := make([]string, 0, len(theSection))
|
|
|
|
for key := range theSection {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
return keys
|
2021-03-10 15:40:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 21:01:06 +00:00
|
|
|
// GetValue returns the key in section with a found flag.
|
|
|
|
func (s *defaultStorage) GetValue(section string, key string) (value string, found bool) {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
theSection, hasSection := s.sections[section]
|
|
|
|
if !hasSection {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
value, hasValue := theSection[key]
|
|
|
|
return value, hasValue
|
2021-03-10 15:40:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 21:01:06 +00:00
|
|
|
func (s *defaultStorage) SetValue(section string, key string, value string) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
theSection, hasSection := s.sections[section]
|
|
|
|
if !hasSection {
|
|
|
|
theSection = map[string]string{}
|
|
|
|
s.sections[section] = theSection
|
|
|
|
}
|
|
|
|
theSection[key] = value
|
2021-03-10 15:40:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 21:01:06 +00:00
|
|
|
func (s *defaultStorage) DeleteKey(section string, key string) bool {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
theSection, hasSection := s.sections[section]
|
|
|
|
if !hasSection {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, hasKey := theSection[key]
|
|
|
|
if !hasKey {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
delete(s.sections[section], key)
|
|
|
|
return true
|
2021-03-10 15:40:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 21:01:06 +00:00
|
|
|
func (s *defaultStorage) Load() error {
|
|
|
|
return nil
|
2021-03-10 15:40:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 21:01:06 +00:00
|
|
|
func (s *defaultStorage) Save() error {
|
|
|
|
return nil
|
2021-03-10 15:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize the config into a string
|
2021-05-03 21:01:06 +00:00
|
|
|
func (s *defaultStorage) Serialize() (string, error) {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
j, err := json.Marshal(s.sections)
|
|
|
|
return string(j), err
|
2021-03-10 15:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interface is satisfied
|
2021-05-03 21:01:06 +00:00
|
|
|
var _ Storage = newDefaultStorage()
|