Switch main configuration language to Lua (fix #28)

This commit is contained in:
Noah 2024-03-27 11:25:55 -05:00
parent 2edf00073f
commit 84961bda58
4 changed files with 71 additions and 23 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
lapisdeploy lapisdeploy
config.json config.json
config.lua
sites/ sites/

View File

@ -2,14 +2,14 @@
package main package main
import ( import (
"encoding/json" "errors"
"log"
"os" "github.com/yuin/gopher-lua"
) )
// Configuration stores information retrieved from a configuration file // Configuration stores information retrieved from a configuration file
type Configuration struct { type Configuration struct {
port int port int64
sites_dir string sites_dir string
matrix struct { matrix struct {
homeserver string homeserver string
@ -19,23 +19,64 @@ type Configuration struct {
} }
} }
// 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))
}
// parseConfig parses the JSON configuration file at 'path' and stores the contents in 'config' // parseConfig parses the JSON configuration file at 'path' and stores the contents in 'config'
func parseConfig(path string, config *Configuration) { func parseConfig(path string, config *Configuration) {
file, _ := os.Open(path) // Lua state setup
var data map[string]interface{} L := lua.NewState()
err := json.NewDecoder(file).Decode(&data) defer L.Close()
if err != nil {
log.Panic("Failed to parse config: ", err)
}
file.Close()
// Main configuration
config.port = int(data["port"].(float64)) // this conversion makes no sense
config.sites_dir = data["sites_dir"].(string)
// Matrix configuration L.DoFile(path)
matrix := data["matrix"].(map[string]interface{}) table := L.Get(-1) // Get the table returned by the config file
config.matrix.homeserver = matrix["homeserver"].(string)
config.matrix.username = matrix["username"].(string) // Main config
config.matrix.password = matrix["password"].(string) if err := getStringFromTable(L, table, "sites_dir", &config.sites_dir); err != nil {
config.matrix.room_id = matrix["room_id"].(string) 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)
}
} }

10
go.mod
View File

@ -3,8 +3,13 @@ module code.retroedge.tech/noah/lapisdeploy
go 1.22.0 go 1.22.0
require ( require (
github.com/akamensky/argparse v1.4.0 // indirect github.com/akamensky/argparse v1.4.0
github.com/gogs/git-module v1.8.3 // indirect github.com/gogs/git-module v1.8.3
github.com/yuin/gopher-lua v1.1.1
maunium.net/go/mautrix v0.17.0
)
require (
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75 // indirect github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75 // indirect
@ -22,5 +27,4 @@ require (
golang.org/x/sys v0.18.0 // indirect golang.org/x/sys v0.18.0 // indirect
golang.org/x/tools v0.19.0 // indirect golang.org/x/tools v0.19.0 // indirect
maunium.net/go/maulogger/v2 v2.4.1 // indirect maunium.net/go/maulogger/v2 v2.4.1 // indirect
maunium.net/go/mautrix v0.17.0 // indirect
) )

2
go.sum
View File

@ -37,6 +37,8 @@ github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68= github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68=
github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
go.mau.fi/util v0.4.0 h1:S2X3qU4pUcb/vxBRfAuZjbrR9xVMAXSjQojNBLPBbhs= go.mau.fi/util v0.4.0 h1:S2X3qU4pUcb/vxBRfAuZjbrR9xVMAXSjQojNBLPBbhs=
go.mau.fi/util v0.4.0/go.mod h1:leeiHtgVBuN+W9aDii3deAXnfC563iN3WK6BF8/AjNw= go.mau.fi/util v0.4.0/go.mod h1:leeiHtgVBuN+W9aDii3deAXnfC563iN3WK6BF8/AjNw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=