|
|
@@ -1,10 +1,13 @@
|
|
|
package config
|
|
|
|
|
|
+// functions related to managing urbit config jsons & corresponding structs
|
|
|
+
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"goseg/structs"
|
|
|
"io/ioutil"
|
|
|
+ "os"
|
|
|
"path/filepath"
|
|
|
"sync"
|
|
|
)
|
|
|
@@ -14,12 +17,14 @@ var (
|
|
|
urbitMutex sync.RWMutex
|
|
|
)
|
|
|
|
|
|
+// retrieve struct corresponding with urbit json file
|
|
|
func UrbitConf(pier string) structs.UrbitDocker {
|
|
|
urbitMutex.Lock()
|
|
|
defer urbitMutex.Unlock()
|
|
|
return UrbitsConfig[pier]
|
|
|
}
|
|
|
|
|
|
+// load urbit conf json into memory
|
|
|
func LoadUrbitConfig(pier string) error {
|
|
|
urbitMutex.Lock()
|
|
|
defer urbitMutex.Unlock()
|
|
|
@@ -28,7 +33,8 @@ func LoadUrbitConfig(pier string) error {
|
|
|
file, err := ioutil.ReadFile(confPath)
|
|
|
if err != nil {
|
|
|
errmsg := fmt.Sprintf("Unable to load %s config: %v", pier, err)
|
|
|
- return fmt.Errorf(errmsg) // Return an error instead of a string
|
|
|
+ return fmt.Errorf(errmsg)
|
|
|
+ // todo: write a new conf
|
|
|
}
|
|
|
// Unmarshal JSON
|
|
|
var targetStruct structs.UrbitDocker
|
|
|
@@ -40,3 +46,29 @@ func LoadUrbitConfig(pier string) error {
|
|
|
UrbitsConfig[pier] = targetStruct
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+// update the in-memory struct and save it to json
|
|
|
+func UpdateUrbitConfig(inputConfig map[string]structs.UrbitDocker) error {
|
|
|
+ urbitMutex.Lock()
|
|
|
+ defer urbitMutex.Unlock()
|
|
|
+ // update UrbitsConfig with the values from inputConfig
|
|
|
+ for pier, config := range inputConfig {
|
|
|
+ UrbitsConfig[pier] = config
|
|
|
+ // also update the corresponding json files
|
|
|
+ path := filepath.Join(BasePath, "settings", "pier", pier+".json")
|
|
|
+ if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ file, err := os.Create(path)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ defer file.Close()
|
|
|
+ encoder := json.NewEncoder(file)
|
|
|
+ encoder.SetIndent("", " ")
|
|
|
+ if err := encoder.Encode(&config); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|