LapisDeploy/config.go

99 lines
3.2 KiB
Go

// package main is the main package for the LapisDeploy program
package main
import (
"errors"
"log"
lua "github.com/yuin/gopher-lua"
)
// Configuration stores information retrieved from a configuration file
type Configuration struct {
port int64
sites_dir string
matrix struct {
homeserver string
username string
password string
room_id string
}
}
// getStringFromTable retrieves a string from a table on the lua stack using the key passed in.
//
// If the value is nil, 'out' is unchanged.
func getStringFromTable(L *lua.LState, lobj lua.LValue, key string, out *string) (bool, error) {
lv := L.GetTable(lobj, lua.LString(key))
if text, ok := lv.(lua.LString); ok { // Value is string
*out = string(text)
} 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")
}
return true, nil
}
// getIntFromTable retrieves an integer from a table on the Lua stack using the key passed in.
//
// If the value is nil, 'out' is unchanged.
func getIntFromTable(L *lua.LState, lobj lua.LValue, key string, out *int64) (bool, error) {
lv := L.GetTable(lobj, lua.LString(key))
if int, ok := lv.(lua.LNumber); ok { // Value is number
*out = int64(int)
} 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")
}
return true, 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) bool {
lv := L.GetTable(lobj, lua.LString(key))
*out = lv
if _, ok := lv.(*lua.LNilType); ok {
return false
}
return true
}
// parseConfig parses the JSON configuration file at 'path' and stores the contents in 'config'
func parseConfig(path string, config *Configuration) {
log.Printf("Loading config from : %s ...", path)
// Lua state setup
L := lua.NewState()
defer L.Close()
L.DoFile(path)
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())
}
// 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) // Get the 'matrix' sub table
if matrix.Type().String() != "table" { // Make sure 'config.matrix' is of type 'table'
log.Fatalf("config.matrix is of type : '%s', not 'table'", matrix.Type().String())
}
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)
}
}