feat: add prependPath function (resolves #81)

dev
TorchedSammy 2021-11-22 12:46:39 -05:00
parent 72cf776882
commit 94a8b4ad47
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 16 additions and 0 deletions

16
lua.go
View File

@ -32,6 +32,7 @@ func LuaInit() {
l.SetGlobal("multiprompt", l.NewFunction(hshmlprompt))
l.SetGlobal("alias", l.NewFunction(hshalias))
l.SetGlobal("appendPath", l.NewFunction(hshappendPath))
l.SetGlobal("prependPath", l.NewFunction(hshprependPath))
l.SetGlobal("exec", l.NewFunction(hshexec))
l.SetGlobal("goro", luar.New(l, hshgoroutine))
l.SetGlobal("timeout", luar.New(l, hshtimeout))
@ -226,3 +227,18 @@ func hshcomplete(L *lua.LState) int {
return 0
}
// prependPath(dir)
// Prepends `dir` to $PATH
func hshprependPath(L *lua.LState) int {
dir := L.CheckString(1)
dir = strings.Replace(dir, "~", curuser.HomeDir, 1)
pathenv := os.Getenv("PATH")
// if dir isnt already in $PATH, add in
if !strings.Contains(pathenv, dir) {
os.Setenv("PATH", dir + string(os.PathListSeparator) + pathenv)
}
return 0
}