summaryrefslogtreecommitdiff
path: root/etc.go
diff options
context:
space:
mode:
Diffstat (limited to 'etc.go')
-rw-r--r--etc.go59
1 files changed, 59 insertions, 0 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)
+ }
+}