summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-28 04:35:07 -0500
committerJeff Carr <[email protected]>2025-10-28 04:35:07 -0500
commit05d09b7584b4cd18676c68dd161142044437a7af (patch)
tree59ada03a3c607f6d7a383da4adcedf5654fa202c
parent59effd5ceba6f28e2bc2edd2b927abb4556f63b5 (diff)
load custom env files
-rw-r--r--README.md8
-rw-r--r--addpath.go27
-rw-r--r--etc.go4
-rw-r--r--init.go11
-rw-r--r--path.go49
5 files changed, 66 insertions, 33 deletions
diff --git a/README.md b/README.md
index 2e97173..33bc4c0 100644
--- a/README.md
+++ b/README.md
@@ -16,10 +16,10 @@ The core philosophy of this library is to standardize env in a new way
env configuration files are loaded in this defined order:
-1. /usr/share/doc/foo/foo.env # upstream application defaults
-2. /etc/foo/foo.env # OS distribution defaults
-3. /etc/default/foo # system admin overrides
-4. ~/.config/foo/foo.env # your user settings
+1. /usr/share/doc/foo/foorc # upstream application defaults
+2. /etc/foo.d/ # OS distribution defaults
+3. /etc/default/foo # system admin overrides
+4. ~/.config/foo/foorc # your user settings
## function examples
diff --git a/addpath.go b/addpath.go
deleted file mode 100644
index 22f6c1e..0000000
--- a/addpath.go
+++ /dev/null
@@ -1,27 +0,0 @@
-package env
-
-import (
- "os"
- "strings"
-
- "go.wit.com/log"
-)
-
-// this is an experiment at this point to
-// see how this turns out
-
-// adds a path to the ENV
-func AddPath(newpath string) bool {
- path := os.Getenv("PATH")
- for _, p := range strings.Split(path, ":") {
- log.Info("Looking at path:", p)
- if p == newpath {
- log.Info("FOUND path:", p)
- return false
- }
- }
- path = path + ":" + newpath
- log.Info("ADDING PATH:", path)
- os.Setenv("PATH", path)
- return true
-}
diff --git a/etc.go b/etc.go
index 00ae426..4e7e7ec 100644
--- a/etc.go
+++ b/etc.go
@@ -32,14 +32,14 @@ func LoadEtc() error {
for _, file := range files {
data, _ := os.ReadFile(file)
_, name := filepath.Split(file)
- addEtcFile("etc/"+name, string(data))
+ parseFileData("etc/"+name, string(data))
log.Info("LoadEtc() file:", file)
}
return nil
}
-func addEtcFile(global string, data string) {
+func parseFileData(global string, data string) {
for _, line := range strings.Split(data, "\n") {
// chop spaces and quotes. similar rules to bash ENV
line = strings.TrimSpace(line)
diff --git a/init.go b/init.go
index ab1ccbf..4db6315 100644
--- a/init.go
+++ b/init.go
@@ -1,6 +1,7 @@
package env
import (
+ "fmt"
"os"
"os/user"
"path/filepath"
@@ -120,3 +121,13 @@ func parseENV(data string) {
envPB.Append(c)
}
}
+
+func LoadENV(filename string) error {
+ fpath := FullPath(filename)
+ fmt.Println("LoadENV", fpath)
+ data, err := os.ReadFile(fpath)
+ if err == nil {
+ parseFileData(filename, string(data))
+ }
+ return err
+}
diff --git a/path.go b/path.go
new file mode 100644
index 0000000..66a27fe
--- /dev/null
+++ b/path.go
@@ -0,0 +1,49 @@
+package env
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+
+ "go.wit.com/log"
+)
+
+// adds a path to the ENV
+func AddPath(newpath string) bool {
+ path := os.Getenv("PATH")
+ for _, p := range strings.Split(path, ":") {
+ log.Info("Looking at path:", p)
+ if p == newpath {
+ log.Info("FOUND path:", p)
+ return false
+ }
+ }
+ path = path + ":" + newpath
+ log.Info("ADDING PATH:", path)
+ os.Setenv("PATH", path)
+ return true
+}
+
+// for "/home/turing/bletchley" returns "~/bletchley"
+func RelPath(p string) string {
+ p = strings.TrimSpace(p)
+ p = strings.Trim(p, "\"'")
+ homedir := Get("homeDir")
+ if strings.HasPrefix(p, homedir) {
+ p = strings.TrimPrefix(p, homedir)
+ p = filepath.Join("~", p)
+ }
+ return p
+}
+
+// for "~/bletchley" returns "/home/turing/bletchley"
+func FullPath(p string) string {
+ p = strings.TrimSpace(p)
+ p = strings.Trim(p, "\"'")
+ homedir := Get("homeDir")
+ if strings.HasPrefix(p, "~") {
+ p = strings.TrimPrefix(p, "~")
+ p = filepath.Join(homedir, p)
+ }
+ return p
+}