refactor: make `hilbish` an already required module and add run function

idk how to explain this man
in code `hilbish` is now a module, which can also be required like
others. but it is already a global export as the variable of the same
name.
also
pull/59/head
sammyette 2021-05-16 17:13:28 -04:00
parent c6aa5fa387
commit ef1ced4a04
No known key found for this signature in database
GPG Key ID: 50EE40A2809851F5
3 changed files with 42 additions and 15 deletions

View File

@ -3,9 +3,44 @@
// go here.
package main
type Hilbish struct {
Version string // Hilbish's version
User string // Name of the user
Hostname string
import (
"os"
"github.com/yuin/gopher-lua"
"mvdan.cc/sh/v3/interp"
)
var exports = map[string]lua.LGFunction {
"run": run,
}
func HilbishLoader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), exports)
host, _ := os.Hostname()
L.SetField(mod, "ver", lua.LString(version))
L.SetField(mod, "user", lua.LString(curuser.Username))
L.SetField(mod, "host", lua.LString(host))
L.Push(mod)
return 1
}
// Runs a command
func run(L *lua.LState) int {
var exitcode uint8 = 0
cmd := L.CheckString(1)
err := execCommand(cmd)
if code, ok := interp.IsExitStatus(err); ok {
exitcode = code
} else if err != nil {
exitcode = 1
}
L.Push(lua.LNumber(exitcode))
return 1
}

5
lua.go
View File

@ -12,7 +12,6 @@ import (
"hilbish/golibs/fs"
"github.com/yuin/gopher-lua"
"layeh.com/gopher-luar"
)
var minimalconf = `
@ -32,7 +31,9 @@ func LuaInit() {
l.SetGlobal("appendPath", l.NewFunction(hshappendPath))
l.SetGlobal("exec", l.NewFunction(hshexec))
l.SetGlobal("hilbish", luar.New(l, hilbish))
// yes this is stupid, i know
l.PreloadModule("hilbish", HilbishLoader)
l.DoString("hilbish = require 'hilbish'")
// Add fs module to Lua
l.PreloadModule("fs", fs.Loader)

View File

@ -37,7 +37,6 @@ var (
interactive bool
login bool // Are we the login shell?
noexecute bool // Should we run Lua or only report syntax errors
hilbish *Hilbish // dont ask
)
func main() {
@ -109,14 +108,6 @@ func main() {
}
}
host, _ := os.Hostname()
hilbish = &Hilbish {
Version: version,
User: curuser.Username,
Hostname: host,
}
go HandleSignals()
LuaInit()
RunLogin()