From 84961bda58bfaa84846abb5999138eebfec6bf0e Mon Sep 17 00:00:00 2001 From: Noah Date: Wed, 27 Mar 2024 11:25:55 -0500 Subject: [PATCH] Switch main configuration language to Lua (fix #28) --- .gitignore | 1 + config.go | 81 ++++++++++++++++++++++++++++++++++++++++-------------- go.mod | 10 +++++-- go.sum | 2 ++ 4 files changed, 71 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index c5dfc4a..8c58e69 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ lapisdeploy config.json +config.lua sites/ diff --git a/config.go b/config.go index b4ce5bb..9af80f7 100644 --- a/config.go +++ b/config.go @@ -2,14 +2,14 @@ package main import ( - "encoding/json" - "log" - "os" + "errors" + + "github.com/yuin/gopher-lua" ) // Configuration stores information retrieved from a configuration file type Configuration struct { - port int + port int64 sites_dir string matrix struct { 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' func parseConfig(path string, config *Configuration) { - file, _ := os.Open(path) - var data map[string]interface{} - err := json.NewDecoder(file).Decode(&data) - 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) + // Lua state setup + L := lua.NewState() + defer L.Close() - // Matrix configuration - matrix := data["matrix"].(map[string]interface{}) - config.matrix.homeserver = matrix["homeserver"].(string) - config.matrix.username = matrix["username"].(string) - config.matrix.password = matrix["password"].(string) - config.matrix.room_id = matrix["room_id"].(string) + 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) + } } diff --git a/go.mod b/go.mod index ee697cb..d2f839a 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,13 @@ module code.retroedge.tech/noah/lapisdeploy go 1.22.0 require ( - github.com/akamensky/argparse v1.4.0 // indirect - github.com/gogs/git-module v1.8.3 // indirect + github.com/akamensky/argparse v1.4.0 + 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-isatty v0.0.20 // 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/tools v0.19.0 // indirect maunium.net/go/maulogger/v2 v2.4.1 // indirect - maunium.net/go/mautrix v0.17.0 // indirect ) diff --git a/go.sum b/go.sum index d4869ba..d6a6ff6 100644 --- a/go.sum +++ b/go.sum @@ -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/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68= 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/go.mod h1:leeiHtgVBuN+W9aDii3deAXnfC563iN3WK6BF8/AjNw= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=