summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-26 20:03:39 -0500
committerJeff Carr <[email protected]>2025-10-26 20:03:39 -0500
commit59c9500872620a0e43752d7ebf352804aaf49925 (patch)
tree8b650df674fcbe1d06892d27e54d4671b4b9d594
parent625fee5541cfce6afb7cadaa4f661355a063c67a (diff)
add something to read from /etc/<appname>.d/
-rw-r--r--etc.go59
-rw-r--r--init.go18
-rw-r--r--panic.go2
3 files changed, 70 insertions, 9 deletions
diff --git a/etc.go b/etc.go
new file mode 100644
index 0000000..339ba43
--- /dev/null
+++ b/etc.go
@@ -0,0 +1,59 @@
+package env
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+
+ "go.wit.com/log"
+)
+
+var ErrInit error = fmt.Errorf("lib/env has not been initialized")
+
+func LoadEtc() error {
+ log.Info("Starting LoadEtc() with appname:", APPNAME)
+ if envPB == nil {
+ return ErrInit
+ }
+ if APPNAME == "" {
+ return ErrInit
+ }
+
+ globPattern := filepath.Join("/etc/", APPNAME+".d", "*")
+ log.Info("glob Pattern:", globPattern)
+ files, err := filepath.Glob(globPattern)
+ if err != nil {
+ fmt.Printf("%s glob error (%v)\n", globPattern, err)
+ return err
+ }
+ log.Info("found files:", files)
+
+ for _, file := range files {
+ data, _ := os.ReadFile(file)
+ _, name := filepath.Split(file)
+ addEtcFile("etc/"+name, string(data))
+ log.Info("LoadEtc() file:", file)
+ }
+
+ return nil
+}
+
+func addEtcFile(global string, data string) {
+ for _, line := range strings.Split(data, "\n") {
+ // chop spaces and quotes. similar rules to bash ENV
+ line = strings.TrimSpace(line)
+ line = strings.Trim(line, "'\"")
+ if line == "" {
+ continue
+ }
+ parts := strings.Split(line, "=")
+ if len(parts) != 2 {
+ // fmt.Println("INVALID LINE:", i, line)
+ continue
+ }
+ varname := parts[0]
+ value := parts[1]
+ SetGlobal(global, varname, value)
+ }
+}
diff --git a/init.go b/init.go
index ac03111..e7130c0 100644
--- a/init.go
+++ b/init.go
@@ -40,15 +40,19 @@ func loadAppENV() error {
return err
}
- envPB.Filename = filepath.Join(configDir, APPNAME, APPNAME+".ENV")
- // fmt.Println("envPB.Filename", envPB.Filename)
-
+ {
+ // old way. deprecate in 2026
+ envPB.Filename = filepath.Join(configDir, APPNAME, APPNAME+".ENV")
+ data, err := os.ReadFile(envPB.Filename)
+ if err == nil {
+ parseENV(string(data))
+ }
+ }
+ envPB.Filename = filepath.Join(configDir, APPNAME, APPNAME+"rc")
data, err := os.ReadFile(envPB.Filename)
- if err != nil {
- return err
+ if err == nil {
+ parseENV(string(data))
}
-
- parseENV(string(data))
return err
}
diff --git a/panic.go b/panic.go
index 46d47ec..e4438db 100644
--- a/panic.go
+++ b/panic.go
@@ -1,8 +1,6 @@
package env
func GetPanic(flag string) string {
- saveMu.Lock()
- defer saveMu.Unlock()
if envPB == nil {
envPanic(flag)
}