feat: allow appendPath to take table of paths (closes #88)

windows-fixes
TorchedSammy 2021-12-31 13:22:26 -04:00
parent 54689b7dd1
commit bcbd4be7f9
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 17 additions and 3 deletions

20
lua.go
View File

@ -137,7 +137,23 @@ func hshalias(L *lua.LState) int {
// appendPath(dir) // appendPath(dir)
// Appends `dir` to $PATH // Appends `dir` to $PATH
func hshappendPath(L *lua.LState) int { func hshappendPath(L *lua.LState) int {
dir := L.CheckString(1) // check if dir is a table or a string
arg := L.Get(1)
fmt.Println(arg.Type())
if arg.Type() == lua.LTTable {
arg.(*lua.LTable).ForEach(func(k lua.LValue, v lua.LValue) {
appendPath(v.String())
})
} else if arg.Type() == lua.LTString {
appendPath(arg.String())
} else {
L.RaiseError("bad argument to appendPath (expected string or table, got %v)", L.Get(1).Type().String())
}
return 0
}
func appendPath(dir string) {
dir = strings.Replace(dir, "~", curuser.HomeDir, 1) dir = strings.Replace(dir, "~", curuser.HomeDir, 1)
pathenv := os.Getenv("PATH") pathenv := os.Getenv("PATH")
@ -145,8 +161,6 @@ func hshappendPath(L *lua.LState) int {
if !strings.Contains(pathenv, dir) { if !strings.Contains(pathenv, dir) {
os.Setenv("PATH", pathenv + string(os.PathListSeparator) + dir) os.Setenv("PATH", pathenv + string(os.PathListSeparator) + dir)
} }
return 0
} }
// exec(cmd) // exec(cmd)