2024-03-08 15:36:11 -06:00
|
|
|
// package main is the main package for the LapisDeploy program
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-03-27 11:25:55 -05:00
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/yuin/gopher-lua"
|
2024-03-08 15:36:11 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Configuration stores information retrieved from a configuration file
|
|
|
|
type Configuration struct {
|
2024-03-27 11:25:55 -05:00
|
|
|
port int64
|
2024-03-08 15:36:11 -06:00
|
|
|
sites_dir string
|
2024-03-15 17:51:45 -05:00
|
|
|
matrix struct {
|
2024-03-10 17:21:27 -05:00
|
|
|
homeserver string
|
2024-03-15 17:51:45 -05:00
|
|
|
username string
|
|
|
|
password string
|
|
|
|
room_id string
|
2024-03-10 17:21:27 -05:00
|
|
|
}
|
2024-03-08 15:36:11 -06:00
|
|
|
}
|
|
|
|
|
2024-03-27 11:25:55 -05:00
|
|
|
// getStringFromTable retrieves a string from a table on the lua stack using the key passed in.
|
|
|
|
func getStringFromTable(L *lua.LState, lobj lua.LValue, key string, out *string) error {
|
|
|
|
lv := L.GetTable(lobj, lua.LString(key))
|
|
|
|
if text, ok := lv.(lua.LString); ok {
|
|
|
|
*out = string(text)
|
|
|
|
} else {
|
|
|
|
return errors.New("Failed to get string from table using key '" + key + "'")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getIntFromTable retrieves an integer from a table on the Lua stack using the key passed in.
|
|
|
|
func getIntFromTable(L *lua.LState, lobj lua.LValue, key string, out *int64) error {
|
|
|
|
lv := L.GetTable(lobj, lua.LString(key))
|
|
|
|
if int, ok := lv.(lua.LNumber); ok {
|
|
|
|
*out = int64(int)
|
|
|
|
} else {
|
|
|
|
return errors.New("Failed to get integer from table using key '" + key + "'")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getTableFromTable retrieves a nested table from the Lua stack using the key passed in.
|
|
|
|
func getTableFromTable(L *lua.LState, lobj lua.LValue, key string, out *lua.LValue) {
|
|
|
|
*out = L.GetTable(lobj, lua.LString(key))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-03-08 15:36:11 -06:00
|
|
|
// parseConfig parses the JSON configuration file at 'path' and stores the contents in 'config'
|
|
|
|
func parseConfig(path string, config *Configuration) {
|
2024-03-27 11:25:55 -05:00
|
|
|
// Lua state setup
|
|
|
|
L := lua.NewState()
|
|
|
|
defer L.Close()
|
|
|
|
|
|
|
|
L.DoFile(path)
|
|
|
|
table := L.Get(-1) // Get the table returned by the config file
|
|
|
|
|
|
|
|
// Main config
|
|
|
|
if err := getStringFromTable(L, table, "sites_dir", &config.sites_dir); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := getIntFromTable(L, table, "port", &config.port); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matrix config
|
|
|
|
var matrix lua.LValue
|
|
|
|
getTableFromTable(L, table, "matrix", &matrix)
|
|
|
|
if err := getStringFromTable(L, matrix, "homeserver", &config.matrix.homeserver); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := getStringFromTable(L, matrix, "username", &config.matrix.username); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := getStringFromTable(L, matrix, "password", &config.matrix.password); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := getStringFromTable(L, matrix, "room_id", &config.matrix.room_id); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-03-08 15:36:11 -06:00
|
|
|
}
|