Let per-site config specify a git branch (fix #30) (WIP)

This commit is contained in:
Noah 2024-04-08 15:29:07 -05:00
parent 401f4a89f8
commit fc91613a66
3 changed files with 52 additions and 23 deletions

View File

@ -4,7 +4,7 @@ package main
import (
"errors"
"github.com/yuin/gopher-lua"
lua "github.com/yuin/gopher-lua"
)
// Configuration stores information retrieved from a configuration file
@ -20,31 +20,39 @@ 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 {
func getStringFromTable(L *lua.LState, lobj lua.LValue, key string, out *string) (bool, error) {
lv := L.GetTable(lobj, lua.LString(key))
if text, ok := lv.(lua.LString); ok {
if text, ok := lv.(lua.LString); ok { // Value is string
*out = string(text)
} else {
return errors.New("Failed to get string from table using key '" + key + "'")
} else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a string, and is not nil
return true, errors.New("'" + key + "' is not a string")
} else { // Value is nil
return false, errors.New("'" + key + "' is nil")
}
return nil
return true, 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 {
func getIntFromTable(L *lua.LState, lobj lua.LValue, key string, out *int64) (bool, error) {
lv := L.GetTable(lobj, lua.LString(key))
if int, ok := lv.(lua.LNumber); ok {
if int, ok := lv.(lua.LNumber); ok { // Value is number
*out = int64(int)
} else {
return errors.New("Failed to get integer from table using key '" + key + "'")
} else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a number, and is not nil
return true, errors.New("'" + key + "' is not a number")
} else { // Value is nil
return false, errors.New("'" + key + "' is nil")
}
return nil
return true, 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))
func getTableFromTable(L *lua.LState, lobj lua.LValue, key string, out *lua.LValue) bool {
lv := L.GetTable(lobj, lua.LString(key))
*out = lv
if _, ok := lv.(*lua.LNilType); ok {
return false
}
return true
}
// parseConfig parses the JSON configuration file at 'path' and stores the contents in 'config'
@ -57,26 +65,26 @@ func parseConfig(path string, config *Configuration) {
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 {
if _, err := getStringFromTable(L, table, "sites_dir", &config.sites_dir); err != nil {
panic(err)
}
if err := getIntFromTable(L, table, "port", &config.port); err != nil {
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 {
if _, err := getStringFromTable(L, matrix, "homeserver", &config.matrix.homeserver); err != nil {
panic(err)
}
if err := getStringFromTable(L, matrix, "username", &config.matrix.username); err != nil {
if _, err := getStringFromTable(L, matrix, "username", &config.matrix.username); err != nil {
panic(err)
}
if err := getStringFromTable(L, matrix, "password", &config.matrix.password); err != nil {
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 {
if _, err := getStringFromTable(L, matrix, "room_id", &config.matrix.room_id); err != nil {
panic(err)
}
}

View File

@ -45,6 +45,11 @@ func handler(data map[string]interface{}) {
deploy_error.SendOverMatrix()
return
}
new_site, exists, err := getSite(site.name)
if err != nil || !exists {
newDeployError(1, "main", "Failed to update site data on a site we just created!", fmt.Sprint(err))
}
site = new_site
sendMessage(MatrixMessage{text: "⚪️ Restarting server..."})
if deploy_error := site.Restart(); deploy_error.code != 0 {
deploy_error.SendOverMatrix()

22
site.go
View File

@ -24,6 +24,9 @@ type Site struct {
type SiteConfig struct {
port int64
name string
git struct {
branch string
}
}
// getSite gets a specific site and returns a Site struct and a bool indicating whether a site was found.
@ -57,11 +60,24 @@ func (site *Site) getConfig() (bool, error) {
table := L.Get(-1) // Get the table returned by the config file
// Parse the config
var config SiteConfig
config := SiteConfig{}
log.Printf("Loading per-site config %s", path)
getStringFromTable(L, table, "name", &config.name)
getIntFromTable(L, table, "port", &config.port)
warning := fmt.Sprintf("🟠 %s : Site config warning : ", site.getName())
if _, err := getStringFromTable(L, table, "name", &config.name); err != nil { // Name
sendMessage(MatrixMessage{text: warning + fmt.Sprint(err)})
}
if _, err := getIntFromTable(L, table, "port", &config.port); err != nil { // Port
sendMessage(MatrixMessage{text: warning + fmt.Sprint(err)})
}
raw_gitt := L.GetTable(table, lua.LString("git")) // Attempt to get 'git' table
if gitt, ok := raw_gitt.(*lua.LTable); ok {
getStringFromTable(L, gitt, "branch", &config.git.branch)
} else if _, ok := raw_gitt.(*lua.LNilType); !ok { // If 'git' is neither a table or nil (undefined),
sendMessage(MatrixMessage{text: warning + "'git' is neither a table or nil!"})
}
log.Print(config)
site.config = config