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!"
}
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*"

31
site.go
View File

@ -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