From fc4f0affc2342dbf0e374ac68837e5c27c7d1377 Mon Sep 17 00:00:00 2001 From: Noah Date: Wed, 27 Mar 2024 11:44:09 -0500 Subject: [PATCH] Switch per-site configuration to Lua (fix #31) --- matrix.go | 2 +- site.go | 31 +++++++++++++++++-------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/matrix.go b/matrix.go index b5aedd9..380bfbb 100644 --- a/matrix.go +++ b/matrix.go @@ -123,7 +123,7 @@ func initMatrix() { return text + "🔴 Failed to get sites!" } for _, site := range sites { - text = fmt.Sprintf("%s- %s\n", text, site.name) + text = fmt.Sprintf("%s- %s\n", text, site.getName()) } if len(sites) == 0 { text = text + "*No sites found*" diff --git a/site.go b/site.go index 9f8cea9..042afa1 100644 --- a/site.go +++ b/site.go @@ -2,7 +2,6 @@ package main import ( - "encoding/json" "errors" "fmt" "log" @@ -12,6 +11,7 @@ import ( "syscall" "github.com/gogs/git-module" + lua "github.com/yuin/gopher-lua" ) // Site contains information and methods used for management of a Lapis site. @@ -20,7 +20,7 @@ type Site struct { config SiteConfig } -// SiteConfig contains parsed data from a site's configuration file +// SiteConfig contains parsed data from a site's configuration file. type SiteConfig struct { port int64 name string @@ -42,30 +42,33 @@ func getSite(name string) (Site, bool, error) { } } -// getConfig parses a site's configuration file +// getConfig parses a site's configuration file. func (site *Site) getConfig() (bool, error) { - path := fmt.Sprintf("%s/%s/deploy_config.json", configuration.sites_dir, site.name) + path := fmt.Sprintf("%s/%s/deploy_config.lua", configuration.sites_dir, site.name) + // Make sure config file exists if exists, _ := fileExists(path); !exists { return false, nil } - 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() + // Lua state setup + L := lua.NewState() + defer L.Close() + + L.DoFile(path) // Load the config + table := L.Get(-1) // Get the table returned by the config file + // Parse the config var config SiteConfig - config.name = data["name"].(string) - config.port = int64(data["port"].(float64)) + log.Printf("Loading per-site config %s", path) + + getStringFromTable(L, table, "name", &config.name) + getIntFromTable(L, table, "port", &config.port) site.config = config return true, nil } -// getName gets a website's configured name (falls back to technical name if one isn't configured) +// getName gets a website's configured name (falls back to technical name if one isn't configured). func (site *Site) getName() string { if site.config.name == "" { return site.name