Make sure Lua values are actually tables before accessing them (fix #44)
This commit is contained in:
parent
698d472ca1
commit
476ac5a175
10
config.go
10
config.go
@ -3,6 +3,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
|
|
||||||
lua "github.com/yuin/gopher-lua"
|
lua "github.com/yuin/gopher-lua"
|
||||||
)
|
)
|
||||||
@ -57,12 +58,16 @@ func getTableFromTable(L *lua.LState, lobj lua.LValue, key string, out *lua.LVal
|
|||||||
|
|
||||||
// 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'
|
||||||
func parseConfig(path string, config *Configuration) {
|
func parseConfig(path string, config *Configuration) {
|
||||||
|
log.Printf("Loading config from : %s ...", path)
|
||||||
// Lua state setup
|
// Lua state setup
|
||||||
L := lua.NewState()
|
L := lua.NewState()
|
||||||
defer L.Close()
|
defer L.Close()
|
||||||
|
|
||||||
L.DoFile(path)
|
L.DoFile(path)
|
||||||
table := L.Get(-1) // Get the table returned by the config file
|
table := L.Get(-1) // Get the table returned by the config file
|
||||||
|
if table.Type().String() != "table" { // Make sure value returned is of type 'table'
|
||||||
|
log.Fatalf("config is of type : '%s', not 'table'", table.Type().String())
|
||||||
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
@ -74,7 +79,10 @@ func parseConfig(path string, config *Configuration) {
|
|||||||
|
|
||||||
// Matrix config
|
// Matrix config
|
||||||
var matrix lua.LValue
|
var matrix lua.LValue
|
||||||
getTableFromTable(L, table, "matrix", &matrix)
|
getTableFromTable(L, table, "matrix", &matrix) // Get the 'matrix' sub table
|
||||||
|
if matrix.Type().String() != "table" { // Make sure 'config.matrix' is of type 'table'
|
||||||
|
log.Fatalf("config.matrix is of type : '%s', not 'table'", matrix.Type().String())
|
||||||
|
}
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
22
site.go
22
site.go
@ -48,32 +48,38 @@ 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.lua", configuration.sites_dir, site.name)
|
path := fmt.Sprintf("%s/%s/deploy_config.lua", configuration.sites_dir, site.name)
|
||||||
|
warning := fmt.Sprintf("%s%s : Site config warning : ", Circles["orange"], site.getName())
|
||||||
// Make sure config file exists
|
// Make sure config file exists
|
||||||
if exists, _ := fileExists(path); !exists {
|
if exists, _ := fileExists(path); !exists {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
log.Printf("Loading per-site config %s", path)
|
||||||
// Lua state setup
|
// Lua state setup
|
||||||
L := lua.NewState()
|
L := lua.NewState()
|
||||||
defer L.Close()
|
defer L.Close()
|
||||||
|
|
||||||
L.DoFile(path) // Load the config
|
L.DoFile(path) // Load the config
|
||||||
table := L.Get(-1) // Get the table returned by the config file
|
table := L.Get(-1) // Get the table returned by the config file
|
||||||
|
if table.Type().String() != "table" { // Make sure 'config' is of type 'table'
|
||||||
|
err := fmt.Sprintf("Value returned by config is of type '%s', not 'table'", table.Type().String())
|
||||||
|
sendMessage(MatrixMessage{text: warning + err})
|
||||||
|
return false, errors.New(err)
|
||||||
|
}
|
||||||
|
|
||||||
// Parse the config
|
// Parse the config
|
||||||
config := SiteConfig{}
|
config := SiteConfig{}
|
||||||
log.Printf("Loading per-site config %s", path)
|
|
||||||
|
|
||||||
warning := fmt.Sprintf("%s%s : Site config warning : ", Circles["orange"], site.getName())
|
if _, err := getStringFromTable(L, table, "name", &config.name); err != nil { // name
|
||||||
|
|
||||||
if _, err := getStringFromTable(L, table, "name", &config.name); err != nil { // Name
|
|
||||||
sendMessage(MatrixMessage{text: warning + fmt.Sprint(err)})
|
sendMessage(MatrixMessage{text: warning + fmt.Sprint(err)})
|
||||||
}
|
}
|
||||||
if _, err := getIntFromTable(L, table, "port", &config.port); err != nil { // Port
|
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")) // git
|
||||||
|
if gitt, ok := L.GetTable(table, lua.LString("git")).(*lua.LTable); ok {
|
||||||
|
if _, err := getStringFromTable(L, gitt, "branch", &config.git.branch); err != nil { // git.branch
|
||||||
sendMessage(MatrixMessage{text: warning + fmt.Sprint(err)})
|
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),
|
} 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!"})
|
sendMessage(MatrixMessage{text: warning + "'git' is neither a table or nil!"})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user