diff options
Diffstat (limited to 'src/dkv/api/backendPropertiesConnection.go')
-rw-r--r-- | src/dkv/api/backendPropertiesConnection.go | 62 |
1 files changed, 28 insertions, 34 deletions
diff --git a/src/dkv/api/backendPropertiesConnection.go b/src/dkv/api/backendPropertiesConnection.go index 24140cd..d485b40 100644 --- a/src/dkv/api/backendPropertiesConnection.go +++ b/src/dkv/api/backendPropertiesConnection.go @@ -22,32 +22,28 @@ import ( "io/ioutil" "log" "os" - "sync" ) type KeyValuesInterface interface { - WriteKVsToConsul(string, string) error - ConfigReader(string, string, string) error - ReadMultiplePropertiesRecursive(string) error - ReadMultipleProperties(string) error - ReadProperty(string) error + WriteKVsToConsul(string, string, map[string]string) error + ConfigReader(string, string, string) (map[string]string, error) + ReadMultiplePropertiesRecursive(string, *map[string]string) error + ReadMultipleProperties(string, *map[string]string) error + ReadProperty(string, *map[string]string) error } -type KeyValuesStruct struct { - sync.RWMutex - kvs map[string]string -} +type KeyValuesStruct struct{} var KeyValues KeyValuesInterface -func (kvStruct *KeyValuesStruct) WriteKVsToConsul(token string, subdomain string) error { +func (kvStruct *KeyValuesStruct) WriteKVsToConsul(token string, subdomain string, kvs map[string]string) error { var prefix = "" if subdomain != "" { prefix += token + "/" + subdomain } else { prefix += token + "/" } - for key, value := range kvStruct.kvs { + for key, value := range kvs { key = prefix + key err := Consul.RequestPUT(key, value) if err != nil { @@ -59,50 +55,48 @@ func (kvStruct *KeyValuesStruct) WriteKVsToConsul(token string, subdomain string return nil } -func (kvStruct *KeyValuesStruct) ConfigReader(token string, subdomain string, filename string) error { - defer kvStruct.Unlock() - - kvStruct.Lock() +func (kvStruct *KeyValuesStruct) ConfigReader(token string, subdomain string, filename string) (map[string]string, error) { var filepath = MOUNTPATH + kvs := make(map[string]string) if filename != "" && subdomain != "" { // Specific file in specific domain. filepath += token + "/" + subdomain + "/" + filename - err := kvStruct.ReadProperty(filepath) + err := kvStruct.ReadProperty(filepath, &kvs) if err != nil { - return err + return kvs, err } - return nil + return kvs, nil } if filename != "" && subdomain == "" { // Specific file in Token filepath += token + "/" + filename - err := kvStruct.ReadProperty(filepath) + err := kvStruct.ReadProperty(filepath, &kvs) if err != nil { - return err + return kvs, err } - return nil + return kvs, nil } if filename == "" && subdomain != "" { // All files in specific domain filepath += token + "/" + subdomain - err := kvStruct.ReadMultipleProperties(filepath) + err := kvStruct.ReadMultipleProperties(filepath, &kvs) if err != nil { - return err + return kvs, err } } filepath += token - err := kvStruct.ReadMultiplePropertiesRecursive(filepath) + err := kvStruct.ReadMultiplePropertiesRecursive(filepath, &kvs) if err != nil { - return err + return kvs, err } - return nil + return kvs, nil } -func (kvStruct *KeyValuesStruct) ReadMultiplePropertiesRecursive(path string) error { +func (kvStruct *KeyValuesStruct) ReadMultiplePropertiesRecursive(path string, kvs *map[string]string) error { // Go inside each sub directory and run ReadMultipleProperties inside. files, err := ioutil.ReadDir(path) if err != nil { @@ -112,35 +106,35 @@ func (kvStruct *KeyValuesStruct) ReadMultiplePropertiesRecursive(path string) er for _, f := range files { fi, _ := os.Stat(path + "/" + f.Name()) if fi.Mode().IsDir() { - kvStruct.ReadMultipleProperties(path + "/" + f.Name()) + kvStruct.ReadMultipleProperties(path+"/"+f.Name(), kvs) } else { - kvStruct.ReadProperty(path + "/" + f.Name()) + kvStruct.ReadProperty(path+"/"+f.Name(), kvs) } } return nil } -func (kvStruct *KeyValuesStruct) ReadMultipleProperties(path string) error { +func (kvStruct *KeyValuesStruct) ReadMultipleProperties(path string, kvs *map[string]string) error { files, err := ioutil.ReadDir(path) if err != nil { return err } for _, f := range files { - kvStruct.ReadProperty(path + f.Name()) + kvStruct.ReadProperty(path+f.Name(), kvs) } return nil } -func (kvStruct *KeyValuesStruct) ReadProperty(path string) error { +func (kvStruct *KeyValuesStruct) ReadProperty(path string, kvs *map[string]string) error { _, err := os.Stat(path) if err != nil { return errors.New("File does not exists.") } p := properties.MustLoadFile(path, properties.UTF8) for _, key := range p.Keys() { - kvStruct.kvs[key] = p.MustGet(key) + (*kvs)[key] = p.MustGet(key) } return nil } |