From 698d472ca13bc21a6b9955ed5509c44e6a85c26d Mon Sep 17 00:00:00 2001 From: Noah Date: Mon, 15 Apr 2024 16:09:48 -0500 Subject: [PATCH] Allow using "all" as an argument to !stop / !start / !restart (fix #39) --- matrix_commands.go | 99 ++++++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 34 deletions(-) diff --git a/matrix_commands.go b/matrix_commands.go index 314f947..d6f8ab3 100644 --- a/matrix_commands.go +++ b/matrix_commands.go @@ -8,6 +8,32 @@ import ( var Chatcommands = []MatrixCommand{} +// getSitesByString is a helper function for chat commands. +// If text is "all" it will get all sites. If text is a specific site name it will get that site. +// Returns a list of strings that are textual representations of errors. +func getSitesByString(text string) ([]Site, []string) { + errors := []string{} // errors holds any errors we encounter in this function + sites := []Site{} // sites holds every site we need + // Determine which sites to get + if text == "all" { // If we need to get all sites, (!restart all) + var err DeployError + sites, err = getAllSites() // Get all sites + if err.code != 0 { // Check for errors + errors = append(errors, "Failed to get all sites!") + } + } else { // If we need to get a specific site, (!restart LapisDeploy-TestApp) + site, found, err := getSite(text) // (Attempt to) get that site. + // Check if something went wrong (site doesn't exist, error occured) + if err != nil { // Error occured. + errors = append(errors, "Failed to get site "+text+"!") + } else if !found { // Site doesn't exist. + errors = append(errors, "Invalid site.") + } + sites = append(sites, site) // Add this site to the list of sites needed + } + return sites, errors +} + func registerChatCommands() { Chatcommands = []MatrixCommand{ { @@ -45,24 +71,25 @@ func registerChatCommands() { }, { cmd: "restart", - description: "Restart a site.", + description: "Restart either a specific site, or all sites.", callback: func(ctx context.Context, evt *event.Event, args []string) string { - if len(args) < 2 { + if len(args) < 2 { // If no arguments were given return Circles["red"] + "Invalid site." } - site, found, err := getSite(args[1]) - if err != nil { - return Circles["red"] + "Failed to get site " + args[2] + "!" - } else if !found { - return Circles["red"] + "Invalid site." + sites, errors := getSitesByString(args[1]) + text := "" // text contains the entire message that will be sent. + for _, error := range errors { + text += Circles["red"] + error + "\n" } - text := Circles["white"] + "Restarting server..." - derr := site.Restart() - if derr.code != 0 { - derr.SendOverMatrix() - return text + "\n" + Circles["red"] + "Failed to restart site!" + for _, site := range sites { // For every site queued to be restarted, + if err := site.Restart(); err.code != 0 { // Attempt to restart it + text += fmt.Sprintf("%sRestarting '%s'... failed!\n", Circles["red"], site.getName()) + text += err.ToString() + } else { // Otherwise, success. + text += fmt.Sprintf("%sRestarting '%s'... success", Circles["green"], site.getName()) + } } - return text + "\n" + Circles["green"] + "Successfully restarted site." + return text // Return the message. }, }, { @@ -72,19 +99,20 @@ func registerChatCommands() { if len(args) < 2 { return Circles["red"] + "Invalid site." } - site, found, err := getSite(args[1]) - if err != nil { - return Circles["red"] + "Failed to get site " + args[2] + "!" - } else if !found { - return Circles["red"] + "Invalid site." + sites, errors := getSitesByString(args[1]) + text := "" // text contains the entire message that will be sent. + for _, error := range errors { + text += Circles["red"] + error + "\n" } - - text := Circles["white"] + "Starting server..." - if derr := site.Start(); derr.code != 0 { - derr.SendOverMatrix() - return text + "\n" + Circles["red"] + "Failed to stop site!" + for _, site := range sites { + if err := site.Start(); err.code != 0 { + text += fmt.Sprintf("%sStarting '%s'... failed!\n", Circles["red"], site.getName()) + text += err.ToString() + } else { + text += fmt.Sprintf("%sStarting '%s'... success!\n", Circles["green"], site.getName()) + } } - return text + "\n" + Circles["green"] + "Successfully started site." + return text }, }, { @@ -94,18 +122,21 @@ func registerChatCommands() { if len(args) < 2 { return Circles["red"] + "Invalid site." } - site, found, err := getSite(args[1]) - if err != nil { - return Circles["red"] + "Failed to get site " + args[2] + "!" - } else if !found { - return Circles["red"] + "Invalid site." + sites, errors := getSitesByString(args[1]) + text := "" // text contains the entire message that will be sent. + for _, error := range errors { + text += Circles["red"] + error + "\n" } - text := Circles["white"] + "Stopping server..." - if derr := site.Stop(); derr.code != 0 { - derr.SendOverMatrix() - return text + "\n" + Circles["red"] + "Failed to stop site!" + for _, site := range sites { + if err := site.Stop(); err.code != 0 { + text += fmt.Sprintf("%sStopping '%s'... failed!", Circles["red"], site.getName()) + text += err.ToString() + } else { + text += fmt.Sprintf("%sStopping '%s'... success", Circles["green"], site.getName()) + } } - return text + "\n" + Circles["green"] + "Successfully stopped site." + return text + }, }, }