feat: add exec function

this is basically the `exec` "command" in sh
itll replace hilbish with whatever provided
simple usage:
exec 'sleep 100'
pull/59/head
sammy 2021-05-08 11:50:09 -04:00
parent 1bacbfc778
commit 8f94990fc3
No known key found for this signature in database
GPG Key ID: 50EE40A2809851F5
1 changed files with 19 additions and 0 deletions

19
lua.go
View File

@ -3,7 +3,9 @@ package main
import (
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"hilbish/golibs/bait"
"hilbish/golibs/commander"
@ -31,6 +33,7 @@ func LuaInit() {
l.SetGlobal("multiprompt", l.NewFunction(hshmlprompt))
l.SetGlobal("alias", l.NewFunction(hshalias))
l.SetGlobal("appendPath", l.NewFunction(hshappendPath))
l.SetGlobal("exec", l.NewFunction(hshexec))
// Add fs module to Lua
l.PreloadModule("fs", fs.Loader)
@ -127,3 +130,19 @@ func hshappendPath(L *lua.LState) int {
return 0
}
func hshexec(L *lua.LState) int {
cmd := L.CheckString(1)
cmdArgs, _ := splitInput(cmd)
cmdPath, err := exec.LookPath(cmdArgs[0])
if err != nil {
fmt.Println(err)
// if we get here, cmdPath will be nothing
// therefore nothing will run
}
// syscall.Exec requires an absolute path to a binary
// path, args, string slice of environments
syscall.Exec(cmdPath, cmdArgs, os.Environ())
return 0
}