diff options
| author | Jeff Carr <[email protected]> | 2025-10-28 04:35:07 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-28 04:35:07 -0500 |
| commit | 05d09b7584b4cd18676c68dd161142044437a7af (patch) | |
| tree | 59ada03a3c607f6d7a383da4adcedf5654fa202c | |
| parent | 59effd5ceba6f28e2bc2edd2b927abb4556f63b5 (diff) | |
load custom env files
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | addpath.go | 27 | ||||
| -rw-r--r-- | etc.go | 4 | ||||
| -rw-r--r-- | init.go | 11 | ||||
| -rw-r--r-- | path.go | 49 |
5 files changed, 66 insertions, 33 deletions
@@ -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 -} @@ -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) @@ -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 +} @@ -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 +} |
