2021-12-15 00:54:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2021-12-20 02:37:44 +00:00
|
|
|
"sync"
|
2021-12-15 00:54:23 +00:00
|
|
|
|
|
|
|
"github.com/yuin/gopher-lua"
|
|
|
|
)
|
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
var aliases *aliasHandler
|
2021-12-15 00:54:23 +00:00
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
type aliasHandler struct {
|
2021-12-15 00:54:23 +00:00
|
|
|
aliases map[string]string
|
2021-12-20 02:37:44 +00:00
|
|
|
mu *sync.RWMutex
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialize aliases map
|
2022-03-19 16:43:48 +00:00
|
|
|
func newAliases() *aliasHandler {
|
|
|
|
return &aliasHandler{
|
2021-12-15 00:54:23 +00:00
|
|
|
aliases: make(map[string]string),
|
2021-12-20 02:37:44 +00:00
|
|
|
mu: &sync.RWMutex{},
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
func (a *aliasHandler) Add(alias, cmd string) {
|
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
2021-12-20 02:37:44 +00:00
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
a.aliases[alias] = cmd
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
func (a *aliasHandler) All() map[string]string {
|
|
|
|
return a.aliases
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
func (a *aliasHandler) Delete(alias string) {
|
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
2021-12-20 02:37:44 +00:00
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
delete(a.aliases, alias)
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
func (a *aliasHandler) Resolve(cmdstr string) string {
|
|
|
|
a.mu.RLock()
|
|
|
|
defer a.mu.RUnlock()
|
2021-12-20 02:37:44 +00:00
|
|
|
|
2021-12-15 00:54:23 +00:00
|
|
|
args := strings.Split(cmdstr, " ")
|
2022-03-19 16:43:48 +00:00
|
|
|
for a.aliases[args[0]] != "" {
|
|
|
|
alias := a.aliases[args[0]]
|
2021-12-15 00:54:23 +00:00
|
|
|
cmdstr = alias + strings.TrimPrefix(cmdstr, args[0])
|
|
|
|
cmdArgs, _ := splitInput(cmdstr)
|
|
|
|
args = cmdArgs
|
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
if a.aliases[args[0]] == alias {
|
2021-12-15 00:54:23 +00:00
|
|
|
break
|
|
|
|
}
|
2022-03-19 16:43:48 +00:00
|
|
|
if a.aliases[args[0]] != "" {
|
2021-12-15 00:54:23 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmdstr
|
|
|
|
}
|
|
|
|
|
|
|
|
// lua section
|
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
func (a *aliasHandler) Loader(L *lua.LState) *lua.LTable {
|
2021-12-15 00:54:23 +00:00
|
|
|
// create a lua module with our functions
|
|
|
|
hshaliasesLua := map[string]lua.LGFunction{
|
2022-03-19 16:43:48 +00:00
|
|
|
"add": a.luaAdd,
|
|
|
|
"list": a.luaList,
|
|
|
|
"del": a.luaDelete,
|
2021-12-15 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mod := L.SetFuncs(L.NewTable(), hshaliasesLua)
|
|
|
|
|
|
|
|
return mod
|
|
|
|
}
|
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
func (a *aliasHandler) luaAdd(L *lua.LState) int {
|
2021-12-15 00:54:23 +00:00
|
|
|
alias := L.CheckString(1)
|
|
|
|
cmd := L.CheckString(2)
|
2022-03-19 16:43:48 +00:00
|
|
|
a.Add(alias, cmd)
|
2021-12-15 00:54:23 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
func (a *aliasHandler) luaList(L *lua.LState) int {
|
2021-12-15 00:54:23 +00:00
|
|
|
aliasesList := L.NewTable()
|
2022-03-19 16:43:48 +00:00
|
|
|
for k, v := range a.All() {
|
2021-12-15 00:54:23 +00:00
|
|
|
aliasesList.RawSetString(k, lua.LString(v))
|
|
|
|
}
|
|
|
|
|
2021-12-19 16:54:15 +00:00
|
|
|
L.Push(aliasesList)
|
|
|
|
|
2021-12-15 00:54:23 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-03-19 16:43:48 +00:00
|
|
|
func (a *aliasHandler) luaDelete(L *lua.LState) int {
|
2021-12-15 00:54:23 +00:00
|
|
|
alias := L.CheckString(1)
|
2022-03-19 16:43:48 +00:00
|
|
|
a.Delete(alias)
|
2021-12-15 00:54:23 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|