Let per-site config specify a git branch (fix #30) (WIP)
This commit is contained in:
parent
401f4a89f8
commit
fc91613a66
48
config.go
48
config.go
@ -4,7 +4,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/yuin/gopher-lua"
|
lua "github.com/yuin/gopher-lua"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Configuration stores information retrieved from a configuration file
|
// 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.
|
// 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))
|
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)
|
*out = string(text)
|
||||||
} else {
|
} else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a string, and is not nil
|
||||||
return errors.New("Failed to get string from table using key '" + key + "'")
|
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.
|
// 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))
|
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)
|
*out = int64(int)
|
||||||
} else {
|
} else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a number, and is not nil
|
||||||
return errors.New("Failed to get integer from table using key '" + key + "'")
|
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.
|
// 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) {
|
func getTableFromTable(L *lua.LState, lobj lua.LValue, key string, out *lua.LValue) bool {
|
||||||
*out = L.GetTable(lobj, lua.LString(key))
|
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'
|
// 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
|
table := L.Get(-1) // Get the table returned by the config file
|
||||||
|
|
||||||
// Main config
|
// 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)
|
panic(err)
|
||||||
}
|
}
|
||||||
if err := getIntFromTable(L, table, "port", &config.port); err != nil {
|
if _, err := getIntFromTable(L, table, "port", &config.port); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matrix config
|
// Matrix config
|
||||||
var matrix lua.LValue
|
var matrix lua.LValue
|
||||||
getTableFromTable(L, table, "matrix", &matrix)
|
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)
|
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)
|
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)
|
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)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
5
main.go
5
main.go
@ -45,6 +45,11 @@ func handler(data map[string]interface{}) {
|
|||||||
deploy_error.SendOverMatrix()
|
deploy_error.SendOverMatrix()
|
||||||
return
|
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..."})
|
sendMessage(MatrixMessage{text: "⚪️ Restarting server..."})
|
||||||
if deploy_error := site.Restart(); deploy_error.code != 0 {
|
if deploy_error := site.Restart(); deploy_error.code != 0 {
|
||||||
deploy_error.SendOverMatrix()
|
deploy_error.SendOverMatrix()
|
||||||
|
22
site.go
22
site.go
@ -24,6 +24,9 @@ type Site struct {
|
|||||||
type SiteConfig struct {
|
type SiteConfig struct {
|
||||||
port int64
|
port int64
|
||||||
name string
|
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.
|
// 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
|
table := L.Get(-1) // Get the table returned by the config file
|
||||||
|
|
||||||
// Parse the config
|
// Parse the config
|
||||||
var config SiteConfig
|
config := SiteConfig{}
|
||||||
log.Printf("Loading per-site config %s", path)
|
log.Printf("Loading per-site config %s", path)
|
||||||
|
|
||||||
getStringFromTable(L, table, "name", &config.name)
|
warning := fmt.Sprintf("🟠 %s : Site config warning : ", site.getName())
|
||||||
getIntFromTable(L, table, "port", &config.port)
|
|
||||||
|
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
|
site.config = config
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user