diff --git a/config.go b/config.go index 5af2d0b..ca8e30a 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,7 @@ type Configuration struct { username string password string room_id string + disabled bool } } @@ -46,6 +47,19 @@ func getIntFromTable(L *lua.LState, lobj lua.LValue, key string, out *int64) (bo return true, nil } +// getBoolFromTable retrieves a bool from a table on the Lua stack using the key passed in. +// +// If the value is nil, 'out' is unchanged. +func getBoolFromTable(L *lua.LState, lobj lua.LValue, key string, out *bool) (bool, error) { + lv := L.GetTable(lobj, lua.LString(key)) + if boolean, ok := lv.(lua.LBool); ok { // Value is boolean + *out = bool(boolean) + } else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a bool, and is not nil + return true, errors.New("'" + key + "' is not a bool") + } + 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) bool { lv := L.GetTable(lobj, lua.LString(key)) @@ -80,19 +94,28 @@ func parseConfig(path string, config *Configuration) { // Matrix config var matrix lua.LValue getTableFromTable(L, table, "matrix", &matrix) // Get the 'matrix' sub table - if matrix.Type().String() != "table" { // Make sure 'config.matrix' is of type 'table' + // Make sure 'config.matrix' is of type 'table' + if matrix_type := matrix.Type().String(); matrix_type != "table" && matrix_type != "nil" { log.Fatalf("config.matrix is of type : '%s', not 'table'", matrix.Type().String()) - } - if _, err := getStringFromTable(L, matrix, "homeserver", &config.matrix.homeserver); err != nil { - panic(err) - } - if _, err := getStringFromTable(L, matrix, "username", &config.matrix.username); err != nil { - panic(err) - } - 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 { - panic(err) + } else if matrix_type != "nil" { + if _, err := getBoolFromTable(L, matrix, "disabled", &config.matrix.disabled); err != nil { + panic(err) + } + if !config.matrix.disabled { // Only load the rest of the matrix config if matrix isn't disabled + if _, err := getStringFromTable(L, matrix, "homeserver", &config.matrix.homeserver); err != nil { + panic(err) + } + if _, err := getStringFromTable(L, matrix, "username", &config.matrix.username); err != nil { + panic(err) + } + 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 { + panic(err) + } + } + } else if matrix_type == "nil" { // Assume config.matrix.disabled is true if 'matrix' is missing from the config file + config.matrix.disabled = true } } diff --git a/main.go b/main.go index 0851c44..0ac8229 100644 --- a/main.go +++ b/main.go @@ -105,6 +105,8 @@ func main() { } parseConfig(*config_path, &configuration) // Parse JSON configuration file + // Disable Matrix if it's disabled in either the config or specified on CLI + configuration.matrix.disabled = *disable_matrix || configuration.matrix.disabled http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Webhook receiver var data map[string]interface{} @@ -124,8 +126,8 @@ func main() { log.Printf("Starting Lapis Deploy on port %d...", configuration.port) - startAllSites() // Start all the servers - if !*disable_matrix { // Only start Matrix bot if --disable-matrix isn't passed + startAllSites() // Start all the servers + if !configuration.matrix.disabled { // Only start Matrix bot if --disable-matrix isn't passed go initMatrix() // Start Matrix stuff in a goroutine } else { log.Print("Matrix bot is disabled!")