summaryrefslogtreecommitdiff
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
parente53c5bb205827022eca057327869ff7beb2dd27c (diff)
move in a common place. mutex lock access (seems to work)v0.0.26
-rw-r--r--config.Save.go70
-rw-r--r--flags.go55
2 files changed, 70 insertions, 55 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
+}
diff --git a/flags.go b/flags.go
deleted file mode 100644
index 24a3fce..0000000
--- a/flags.go
+++ /dev/null
@@ -1,55 +0,0 @@
-package config
-
-// returns true if config is working okay
-func InitValid() bool {
- if configPB == nil {
- // todo: try to re-init it here
- return false
- }
- return true
-}
-
-func Get(flag string) string {
- if configPB == nil {
- return ""
- }
- found := configPB.FindByKey(flag)
- if found == nil {
- return ""
- }
- return found.Value
-}
-
-func GetPanic(flag string) string {
- if configPB == nil {
- configPanic(flag)
- }
- found := configPB.FindByKey(flag)
- if found == nil {
- configPanic(flag)
- }
- return found.Value
-}
-
-func configPanic(varname string) {
- if configPB == nil {
- panic("config file is nil")
- }
- panic("config name '" + varname + "' not found")
-}
-
-func Set(key string, newValue string) error {
- 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
-}