github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/fs/config/default_storage.go (about) 1 package config 2 3 import ( 4 "encoding/json" 5 "sync" 6 ) 7 8 // defaultStorage implements config.Storage, providing in-memory config. 9 // Indexed by section, then key. 10 type defaultStorage struct { 11 mu sync.RWMutex 12 sections map[string]map[string]string 13 } 14 15 func newDefaultStorage() *defaultStorage { 16 return &defaultStorage{ 17 sections: map[string]map[string]string{}, 18 } 19 } 20 21 // GetSectionList returns a slice of strings with names for all the sections. 22 func (s *defaultStorage) GetSectionList() []string { 23 s.mu.RLock() 24 defer s.mu.RUnlock() 25 sections := make([]string, 0, len(s.sections)) 26 for section := range s.sections { 27 sections = append(sections, section) 28 } 29 return sections 30 } 31 32 // HasSection returns true if section exists in the config. 33 func (s *defaultStorage) HasSection(section string) bool { 34 s.mu.RLock() 35 defer s.mu.RUnlock() 36 _, hasSection := s.sections[section] 37 return hasSection 38 } 39 40 // DeleteSection deletes the specified section. 41 func (s *defaultStorage) DeleteSection(section string) { 42 s.mu.Lock() 43 defer s.mu.Unlock() 44 delete(s.sections, section) 45 } 46 47 // GetKeyList returns the keys in this section. 48 func (s *defaultStorage) GetKeyList(section string) []string { 49 s.mu.RLock() 50 defer s.mu.RUnlock() 51 theSection := s.sections[section] 52 keys := make([]string, 0, len(theSection)) 53 for key := range theSection { 54 keys = append(keys, key) 55 } 56 return keys 57 } 58 59 // GetValue returns the key in section with a found flag. 60 func (s *defaultStorage) GetValue(section string, key string) (value string, found bool) { 61 s.mu.RLock() 62 defer s.mu.RUnlock() 63 theSection, hasSection := s.sections[section] 64 if !hasSection { 65 return "", false 66 } 67 value, hasValue := theSection[key] 68 return value, hasValue 69 } 70 71 func (s *defaultStorage) SetValue(section string, key string, value string) { 72 s.mu.Lock() 73 defer s.mu.Unlock() 74 theSection, hasSection := s.sections[section] 75 if !hasSection { 76 theSection = map[string]string{} 77 s.sections[section] = theSection 78 } 79 theSection[key] = value 80 } 81 82 func (s *defaultStorage) DeleteKey(section string, key string) bool { 83 s.mu.Lock() 84 defer s.mu.Unlock() 85 theSection, hasSection := s.sections[section] 86 if !hasSection { 87 return false 88 } 89 _, hasKey := theSection[key] 90 if !hasKey { 91 return false 92 } 93 delete(s.sections[section], key) 94 return true 95 } 96 97 func (s *defaultStorage) Load() error { 98 return nil 99 } 100 101 func (s *defaultStorage) Save() error { 102 return nil 103 } 104 105 // Serialize the config into a string 106 func (s *defaultStorage) Serialize() (string, error) { 107 s.mu.RLock() 108 defer s.mu.RUnlock() 109 j, err := json.Marshal(s.sections) 110 return string(j), err 111 } 112 113 // Check the interface is satisfied 114 var _ Storage = newDefaultStorage()