summaryrefslogtreecommitdiff
path: root/config.Save.go
diff options
context:
space:
mode:
Diffstat (limited to 'config.Save.go')
-rw-r--r--config.Save.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/config.Save.go b/config.Save.go
index d7d0bbf..c5aaeb7 100644
--- a/config.Save.go
+++ b/config.Save.go
@@ -3,10 +3,27 @@ package config
import (
"os"
"path/filepath"
+ "sync"
)
+// an experiment to see if this is useful
+var saveMu sync.RWMutex
+
+// returns true if config is working okay
+func InitValid() bool {
+ saveMu.Lock()
+ defer saveMu.Unlock()
+ if configPB == nil {
+ // todo: try to re-init it here
+ return false
+ }
+ return true
+}
+
// saves your applications config file
func Save() error {
+ saveMu.Lock()
+ defer saveMu.Unlock()
basedir, _ := filepath.Split(configPB.Filename)
if err := os.MkdirAll(basedir, os.ModePerm); err != nil {
return err
@@ -14,3 +31,56 @@ func Save() error {
err := SavePB(configPB)
return err
}
+
+func Get(flag string) string {
+ saveMu.Lock()
+ defer saveMu.Unlock()
+ if configPB == nil {
+ return ""
+ }
+ found := configPB.FindByKey(flag)
+ if found == nil {
+ return ""
+ }
+ return found.Value
+}
+
+func GetPanic(flag string) string {
+ saveMu.Lock()
+ defer saveMu.Unlock()
+ if configPB == nil {
+ configPanic(flag)
+ }
+ found := configPB.FindByKey(flag)
+ if found == nil {
+ configPanic(flag)
+ }
+ return found.Value
+}
+
+func configPanic(varname string) {
+ saveMu.Lock()
+ defer saveMu.Unlock()
+ if configPB == nil {
+ panic("config file is nil")
+ }
+ panic("config name '" + varname + "' not found")
+}
+
+func Set(key string, newValue string) error {
+ saveMu.Lock()
+ defer saveMu.Unlock()
+ if configPB == nil {
+ return NotInitialized
+ }
+ found := configPB.FindByKey(key)
+ if found != nil {
+ found.Value = newValue
+ }
+
+ newvar := new(Config)
+ newvar.Key = key
+ newvar.Value = newValue
+ configPB.Append(newvar)
+ return nil
+}