summaryrefslogtreecommitdiff
path: root/config.Save.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-15 12:20:03 -0500
committerJeff Carr <[email protected]>2025-10-15 12:20:03 -0500
commitfab3ce3d8b31ca17a392c34e5fee591ce5ad443f (patch)
tree5aa5f6f7f15e3cdd2a3aa273d6858ef7aac8ffe6 /config.Save.go
parente53c5bb205827022eca057327869ff7beb2dd27c (diff)
move in a common place. mutex lock access (seems to work)v0.0.26
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
+}