summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config.FormatENV.go51
-rw-r--r--config.Load.go76
-rw-r--r--config.Panic.go2
-rw-r--r--config.Save.go47
-rw-r--r--config.proto2
-rw-r--r--init.go44
-rw-r--r--verbose.go4
7 files changed, 179 insertions, 47 deletions
diff --git a/config.FormatENV.go b/config.FormatENV.go
new file mode 100644
index 0000000..bd3a156
--- /dev/null
+++ b/config.FormatENV.go
@@ -0,0 +1,51 @@
+package config
+
+import (
+ "errors"
+ "fmt"
+ "strings"
+
+ "go.wit.com/log"
+)
+
+func formatENV() (string, error) {
+ if configPB == nil {
+ return "", errors.New("configPB not initialized")
+ }
+
+ var out string
+ uniques := make(map[string]*Config) // check to make sure there are no duplicate entries
+
+ for c := range configPB.IterAll() {
+ key := strings.ToLower(c.Key)
+ if len(strings.Fields(key)) != 1 {
+ log.Info("dropping invalid key = ", c.Key, "value =", c.Value)
+ continue
+ }
+ found := findByLower(key)
+ if found == nil {
+ log.Info("findByKey() got nil for key:", key)
+ }
+ // todo: warn about duplicates?
+ uniques[key] = found
+ }
+
+ for key, c := range uniques {
+ if c == nil {
+ log.Info("key has nil c", key)
+ continue
+ }
+ line := fmt.Sprintf("%s=%s", c.Key, c.Value)
+ out += line + "\n"
+ }
+ return out, nil
+}
+
+func findByLower(lookingFor string) *Config {
+ for c := range configPB.IterAll() {
+ if strings.ToLower(c.Key) == strings.ToLower(lookingFor) {
+ return c
+ }
+ }
+ return nil
+}
diff --git a/config.Load.go b/config.Load.go
index b960508..abedc6a 100644
--- a/config.Load.go
+++ b/config.Load.go
@@ -5,33 +5,97 @@ import (
"os"
"os/user"
"path/filepath"
+ "strings"
+
+ "go.wit.com/log"
)
// loads your applications config file from
// ~/.config/<appname>/config.text
func (pb *Config) Load() error {
- appname, err := GetAppname() // already configured by your application
+ filename, err := getConfigFilenameTEXT()
if err != nil {
return err
}
- configdir, err := getConfigDir()
+ _, err = SetFilename(pb, filename)
if err != nil {
return err
}
- filename := filepath.Join(configdir, appname+".text")
- _, err = SetFilename(pb, filename)
+ saveMu.Lock()
+ defer saveMu.Unlock()
+ err = loadTEXT(pb, filename)
+ return err
+}
+
+func loadENV() error {
+ if configPB != nil {
+ log.Info("configPB already loaded")
+ return errors.New("configPB already loaded")
+ }
+ filename, err := getConfigFilenameENV()
+ if err != nil {
+ return err
+ }
+ log.Info("loadENV()", filename)
+ stuff, err := os.ReadFile(filename)
if err != nil {
return err
}
-
saveMu.Lock()
defer saveMu.Unlock()
- err = loadTEXT(pb, filename)
+ configPB = NewConfigs()
+ for i, line := range strings.Split(string(stuff), "\n") {
+ line = strings.TrimSpace(line)
+ if line == "" {
+ continue
+ }
+ parts := strings.Split(line, "=")
+ if len(parts) != 2 {
+ log.Info("INVALID LINE:", i, line)
+ continue
+ }
+ c := new(Config)
+ c.Key = parts[0]
+ c.Value = parts[1]
+ configPB.Append(c)
+ log.Printf("ENV LINE: (%v)\n", c)
+ }
+
return err
}
+func getConfigFilenameENV() (string, error) {
+ appname, err := GetAppname() // already configured by your application
+ if err != nil {
+ return "", err
+ }
+
+ configdir, err := getConfigDir()
+ if err != nil {
+ return "", err
+ }
+
+ filename := filepath.Join(configdir, appname+".ENV")
+ return filename, nil
+}
+
+func getConfigFilenameTEXT() (string, error) {
+ appname, err := GetAppname() // already configured by your application
+ if err != nil {
+ return "", err
+ }
+
+ configdir, err := getConfigDir()
+ if err != nil {
+ return "", err
+ }
+
+ filename := filepath.Join(configdir, appname+".text")
+ return filename, nil
+}
+
func GetAppname() (string, error) {
if APPNAME != "" {
return APPNAME, nil
diff --git a/config.Panic.go b/config.Panic.go
index 45d233e..3e2e246 100644
--- a/config.Panic.go
+++ b/config.Panic.go
@@ -8,7 +8,7 @@ func GetPanic(flag string) string {
if configPB == nil {
configPanic(flag)
}
- found := configPB.FindByKey(flag)
+ found := configPB.findByKey(flag)
if found == nil {
configPanic(flag)
}
diff --git a/config.Save.go b/config.Save.go
index 1f1736f..ecb66ea 100644
--- a/config.Save.go
+++ b/config.Save.go
@@ -2,9 +2,10 @@ package config
import (
"os"
- "path/filepath"
"strings"
"sync"
+
+ "go.wit.com/log"
)
// an experiment to see if this is useful
@@ -23,14 +24,35 @@ func InitValid() bool {
// saves your applications config file
func Save() error {
+ /*
+ basedir, _ := filepath.Split(configPB.Filename)
+ if err := os.MkdirAll(basedir, os.ModePerm); err != nil {
+ return err
+ }
+ saveENV()
+ saveMu.Lock()
+ defer saveMu.Unlock()
+ err := SavePB(configPB)
+ */
+ return saveENV()
+}
+
+func saveENV() error {
+ filename, err := getConfigFilenameENV()
+ if err != nil {
+ panic("saveENV() failed")
+ return err
+ }
saveMu.Lock()
defer saveMu.Unlock()
- basedir, _ := filepath.Split(configPB.Filename)
- if err := os.MkdirAll(basedir, os.ModePerm); err != nil {
- return err
+ outENV, err := formatENV()
+ if err == nil {
+ log.Info("SAVEENV IS RUNNING")
+ log.Info("SAVEENV IS RUNNING")
+ log.Info("SAVEENV IS RUNNING")
+ log.Info(outENV)
}
- err := SavePB(configPB)
- return err
+ return os.WriteFile(filename, []byte(outENV), 0644)
}
func Get(flag string) string {
@@ -47,22 +69,13 @@ func Get(flag string) string {
return c.Value
}
-func findByLower(lookingFor string) *Config {
- for c := range configPB.IterAll() {
- if strings.ToLower(c.Key) == strings.ToLower(lookingFor) {
- return c
- }
- }
- return nil
-}
-
func True(flag string) bool {
saveMu.Lock()
defer saveMu.Unlock()
if configPB == nil {
return false
}
- found := configPB.FindByKey(flag)
+ found := configPB.findByKey(flag)
if found == nil {
return false
}
@@ -78,7 +91,7 @@ func Set(key string, newValue string) error {
if configPB == nil {
return NotInitialized
}
- found := configPB.FindByKey(key)
+ found := configPB.findByKey(key)
if found != nil {
found.Value = newValue
}
diff --git a/config.proto b/config.proto
index 9803223..19e8481 100644
--- a/config.proto
+++ b/config.proto
@@ -9,6 +9,8 @@ import "google/protobuf/timestamp.proto"; // Import the well-known type for Time
message Config { //
string key = 1; // config key name `autogenpb:unique` `autogenpb:sort`
string value = 2; // config value name
+ bool global = 3; // was defined in application OS settings
+ string help = 4; // text for explaining the ENV key/value
}
message Configs { // `autogenpb:marshal` `autogenpb:nomutex`
diff --git a/init.go b/init.go
index a2c146a..5069803 100644
--- a/init.go
+++ b/init.go
@@ -4,7 +4,6 @@ package config
// see how this turns out
import (
- "errors"
"fmt"
"os"
"path/filepath"
@@ -25,28 +24,31 @@ func Init(appname, version, buildtime string, fromargv []string) error {
BUILDTIME = buildtime
argv = fromargv
- configDir, err := os.UserConfigDir()
- if err != nil {
- fmt.Println("OS isn't returning UserConfigDir()", err)
- return err
- }
- fullname := filepath.Join(configDir, appname, "config.text")
- configPB = NewConfigs()
- err = loadTEXT(configPB, fullname)
- if err == nil {
- return nil
- }
+ return loadENV()
+ /*
+ configDir, err := os.UserConfigDir()
+ if err != nil {
+ fmt.Println("OS isn't returning UserConfigDir()", err)
+ return err
+ }
+ fullname := filepath.Join(configDir, appname, "config.text")
+ configPB = NewConfigs()
+ err = loadTEXT(configPB, fullname)
+ if err == nil {
+ return nil
+ }
- if errors.Is(err, os.ErrNotExist) {
- // file doesn't exist, make a new file
- return makeNewConfigFile(appname)
- }
- if errors.Is(err, ErrEmpty) {
- fmt.Printf("config file size was empty. out of diskspace? %s\n", fullname)
+ if errors.Is(err, os.ErrNotExist) {
+ // file doesn't exist, make a new file
+ return makeNewConfigFile(appname)
+ }
+ if errors.Is(err, ErrEmpty) {
+ fmt.Printf("config file size was empty. out of diskspace? %s\n", fullname)
+ return err
+ }
+ fmt.Println("config.Init()", err)
return err
- }
- fmt.Println("config.Init()", err)
- return err
+ */
}
func makeNewConfigFile(appname string) error {
diff --git a/verbose.go b/verbose.go
index d08885c..438475a 100644
--- a/verbose.go
+++ b/verbose.go
@@ -6,7 +6,7 @@ package config
func Verbose() bool {
// always use the config file value first
if configPB != nil {
- found := configPB.FindByKey("Verbose")
+ found := configPB.findByKey("Verbose")
if found == nil {
// Verbose isn't in the config. do nothing here
} else {
@@ -31,7 +31,7 @@ func Verbose() bool {
func If(key string) bool {
// always use the config file value first
if configPB != nil {
- found := configPB.FindByKey(key)
+ found := configPB.findByKey(key)
if found == nil {
// Verbose isn't in the config. do nothing here
} else {