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"
|
2024-04-16 10:00:08 -05:00
|
|
|
"log"
|
2024-03-27 11:25:55 -05:00
|
|
|
|
2024-04-08 15:29:07 -05:00
|
|
|
lua "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-04-16 11:36:51 -05:00
|
|
|
port int64
|
|
|
|
sites_dir string
|
|
|
|
trusted_servers []string
|
|
|
|
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-04-16 10:21:34 -05:00
|
|
|
disabled bool
|
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.
|
2024-04-15 15:10:34 -05:00
|
|
|
//
|
|
|
|
// If the value is nil, 'out' is unchanged.
|
2024-04-08 15:29:07 -05:00
|
|
|
func getStringFromTable(L *lua.LState, lobj lua.LValue, key string, out *string) (bool, error) {
|
2024-03-27 11:25:55 -05:00
|
|
|
lv := L.GetTable(lobj, lua.LString(key))
|
2024-04-08 15:29:07 -05:00
|
|
|
if text, ok := lv.(lua.LString); ok { // Value is string
|
2024-03-27 11:25:55 -05:00
|
|
|
*out = string(text)
|
2024-04-08 15:29:07 -05:00
|
|
|
} else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a string, and is not nil
|
|
|
|
return true, errors.New("'" + key + "' is not a string")
|
2024-03-27 11:25:55 -05:00
|
|
|
}
|
2024-04-08 15:29:07 -05:00
|
|
|
return true, nil
|
2024-03-27 11:25:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// getIntFromTable retrieves an integer from a table on the Lua stack using the key passed in.
|
2024-04-15 15:10:34 -05:00
|
|
|
//
|
|
|
|
// If the value is nil, 'out' is unchanged.
|
2024-04-08 15:29:07 -05:00
|
|
|
func getIntFromTable(L *lua.LState, lobj lua.LValue, key string, out *int64) (bool, error) {
|
2024-03-27 11:25:55 -05:00
|
|
|
lv := L.GetTable(lobj, lua.LString(key))
|
2024-04-08 15:29:07 -05:00
|
|
|
if int, ok := lv.(lua.LNumber); ok { // Value is number
|
2024-03-27 11:25:55 -05:00
|
|
|
*out = int64(int)
|
2024-04-08 15:29:07 -05:00
|
|
|
} else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a number, and is not nil
|
|
|
|
return true, errors.New("'" + key + "' is not a number")
|
2024-03-27 11:25:55 -05:00
|
|
|
}
|
2024-04-08 15:29:07 -05:00
|
|
|
return true, nil
|
2024-03-27 11:25:55 -05:00
|
|
|
}
|
|
|
|
|
2024-04-16 10:21:34 -05:00
|
|
|
// getBoolFromTable retrieves a bool from a table on the Lua stack using the key passed in.
|
|
|
|
//
|
|
|
|
// If the value is nil, 'out' is unchanged.
|
|
|
|
func getBoolFromTable(L *lua.LState, lobj lua.LValue, key string, out *bool) (bool, error) {
|
|
|
|
lv := L.GetTable(lobj, lua.LString(key))
|
|
|
|
if boolean, ok := lv.(lua.LBool); ok { // Value is boolean
|
|
|
|
*out = bool(boolean)
|
|
|
|
} else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a bool, and is not nil
|
|
|
|
return true, errors.New("'" + key + "' is not a bool")
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2024-03-27 11:25:55 -05:00
|
|
|
// getTableFromTable retrieves a nested table from the Lua stack using the key passed in.
|
2024-04-08 15:29:07 -05:00
|
|
|
func getTableFromTable(L *lua.LState, lobj lua.LValue, key string, out *lua.LValue) bool {
|
|
|
|
lv := L.GetTable(lobj, lua.LString(key))
|
|
|
|
*out = lv
|
|
|
|
if _, ok := lv.(*lua.LNilType); ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2024-03-27 11:25:55 -05:00
|
|
|
}
|
|
|
|
|
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-04-16 10:00:08 -05:00
|
|
|
log.Printf("Loading config from : %s ...", path)
|
2024-03-27 11:25:55 -05:00
|
|
|
// Lua state setup
|
|
|
|
L := lua.NewState()
|
|
|
|
defer L.Close()
|
|
|
|
|
|
|
|
L.DoFile(path)
|
2024-04-16 10:00:08 -05:00
|
|
|
table := L.Get(-1) // Get the table returned by the config file
|
|
|
|
if table.Type().String() != "table" { // Make sure value returned is of type 'table'
|
|
|
|
log.Fatalf("config is of type : '%s', not 'table'", table.Type().String())
|
|
|
|
}
|
2024-03-27 11:25:55 -05:00
|
|
|
|
|
|
|
// Main config
|
2024-04-08 15:29:07 -05:00
|
|
|
if _, err := getStringFromTable(L, table, "sites_dir", &config.sites_dir); err != nil {
|
2024-03-27 11:25:55 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
2024-04-08 15:29:07 -05:00
|
|
|
if _, err := getIntFromTable(L, table, "port", &config.port); err != nil {
|
2024-03-27 11:25:55 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-04-16 11:36:51 -05:00
|
|
|
// Trusted servers
|
|
|
|
var trusted_servers lua.LValue
|
|
|
|
getTableFromTable(L, table, "trusted_servers", &trusted_servers) // Get the 'trusted_servers' table
|
|
|
|
// Make sure 'config.trusted_servers' is of type 'table'
|
|
|
|
if trusted_servers_type := trusted_servers.Type().String(); trusted_servers_type != "table" &&
|
|
|
|
trusted_servers_type != "nil" {
|
|
|
|
log.Fatalf("config.trusted_servers is of type : '%s', not 'table'", trusted_servers_type)
|
|
|
|
} else if trusted_servers_type != "nil" { // If type is not nil (has to be table or it would've been caught already)
|
|
|
|
for i := 1; i <= L.ObjLen(trusted_servers); i++ { // Loop through every trusted server
|
|
|
|
lv := L.GetTable(trusted_servers, lua.LNumber(i))
|
|
|
|
if text, ok := lv.(lua.LString); ok { // Value is string
|
|
|
|
config.trusted_servers = append(config.trusted_servers, string(text))
|
|
|
|
} else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a string, and is not nil
|
|
|
|
log.Fatalf("config.trusted_servers[%d] is of type : '%s', not 'string'", i, lv.Type().String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-27 11:25:55 -05:00
|
|
|
// Matrix config
|
|
|
|
var matrix lua.LValue
|
2024-04-16 11:36:51 -05:00
|
|
|
getTableFromTable(L, table, "matrix", &matrix) // Get the 'matrix' table
|
2024-04-16 10:21:34 -05:00
|
|
|
// Make sure 'config.matrix' is of type 'table'
|
|
|
|
if matrix_type := matrix.Type().String(); matrix_type != "table" && matrix_type != "nil" {
|
2024-04-16 11:36:51 -05:00
|
|
|
log.Fatalf("config.matrix is of type : '%s', not 'table'", matrix_type)
|
2024-04-16 10:21:34 -05:00
|
|
|
} else if matrix_type != "nil" {
|
|
|
|
if _, err := getBoolFromTable(L, matrix, "disabled", &config.matrix.disabled); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !config.matrix.disabled { // Only load the rest of the matrix config if matrix isn't disabled
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if matrix_type == "nil" { // Assume config.matrix.disabled is true if 'matrix' is missing from the config file
|
|
|
|
config.matrix.disabled = true
|
2024-03-27 11:25:55 -05:00
|
|
|
}
|
2024-03-08 15:36:11 -06:00
|
|
|
}
|