Switch per-site configuration to Lua (fix #31)

This commit is contained in:
Noah 2024-03-27 11:44:09 -05:00
parent 84961bda58
commit fc4f0affc2
2 changed files with 18 additions and 15 deletions

View File

@ -123,7 +123,7 @@ func initMatrix() {
return text + "🔴 Failed to get sites!" return text + "🔴 Failed to get sites!"
} }
for _, site := range 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 { if len(sites) == 0 {
text = text + "*No sites found*" text = text + "*No sites found*"

31
site.go
View File

@ -2,7 +2,6 @@
package main package main
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"log" "log"
@ -12,6 +11,7 @@ import (
"syscall" "syscall"
"github.com/gogs/git-module" "github.com/gogs/git-module"
lua "github.com/yuin/gopher-lua"
) )
// Site contains information and methods used for management of a Lapis site. // Site contains information and methods used for management of a Lapis site.
@ -20,7 +20,7 @@ type Site struct {
config SiteConfig 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 { type SiteConfig struct {
port int64 port int64
name string 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) { 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 { if exists, _ := fileExists(path); !exists {
return false, nil return false, nil
} }
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) L.DoFile(path) // Load the config
} table := L.Get(-1) // Get the table returned by the config file
file.Close()
// Parse the config // Parse the config
var config SiteConfig var config SiteConfig
config.name = data["name"].(string) log.Printf("Loading per-site config %s", path)
config.port = int64(data["port"].(float64))
getStringFromTable(L, table, "name", &config.name)
getIntFromTable(L, table, "port", &config.port)
site.config = config site.config = config
return true, nil 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 { func (site *Site) getName() string {
if site.config.name == "" { if site.config.name == "" {
return site.name return site.name