diff options
Diffstat (limited to 'path.go')
| -rw-r--r-- | path.go | 49 |
1 files changed, 49 insertions, 0 deletions
@@ -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 +} |
