Allow using "all" as an argument to !stop / !start / !restart (fix #39)

This commit is contained in:
Noah 2024-04-15 16:09:48 -05:00
parent b15b5b7be1
commit 698d472ca1

View File

@ -8,6 +8,32 @@ import (
var Chatcommands = []MatrixCommand{} 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() { func registerChatCommands() {
Chatcommands = []MatrixCommand{ Chatcommands = []MatrixCommand{
{ {
@ -45,24 +71,25 @@ func registerChatCommands() {
}, },
{ {
cmd: "restart", 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 { 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." return Circles["red"] + "Invalid site."
} }
site, found, err := getSite(args[1]) sites, errors := getSitesByString(args[1])
if err != nil { text := "" // text contains the entire message that will be sent.
return Circles["red"] + "Failed to get site " + args[2] + "!" for _, error := range errors {
} else if !found { text += Circles["red"] + error + "\n"
return Circles["red"] + "Invalid site."
} }
text := Circles["white"] + "Restarting server..." for _, site := range sites { // For every site queued to be restarted,
derr := site.Restart() if err := site.Restart(); err.code != 0 { // Attempt to restart it
if derr.code != 0 { text += fmt.Sprintf("%sRestarting '%s'... failed!\n", Circles["red"], site.getName())
derr.SendOverMatrix() text += err.ToString()
return text + "\n" + Circles["red"] + "Failed to restart site!" } 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 { if len(args) < 2 {
return Circles["red"] + "Invalid site." return Circles["red"] + "Invalid site."
} }
site, found, err := getSite(args[1]) sites, errors := getSitesByString(args[1])
if err != nil { text := "" // text contains the entire message that will be sent.
return Circles["red"] + "Failed to get site " + args[2] + "!" for _, error := range errors {
} else if !found { text += Circles["red"] + error + "\n"
return Circles["red"] + "Invalid site."
} }
for _, site := range sites {
text := Circles["white"] + "Starting server..." if err := site.Start(); err.code != 0 {
if derr := site.Start(); derr.code != 0 { text += fmt.Sprintf("%sStarting '%s'... failed!\n", Circles["red"], site.getName())
derr.SendOverMatrix() text += err.ToString()
return text + "\n" + Circles["red"] + "Failed to stop site!" } 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 { if len(args) < 2 {
return Circles["red"] + "Invalid site." return Circles["red"] + "Invalid site."
} }
site, found, err := getSite(args[1]) sites, errors := getSitesByString(args[1])
if err != nil { text := "" // text contains the entire message that will be sent.
return Circles["red"] + "Failed to get site " + args[2] + "!" for _, error := range errors {
} else if !found { text += Circles["red"] + error + "\n"
return Circles["red"] + "Invalid site."
} }
text := Circles["white"] + "Stopping server..." for _, site := range sites {
if derr := site.Stop(); derr.code != 0 { if err := site.Stop(); err.code != 0 {
derr.SendOverMatrix() text += fmt.Sprintf("%sStopping '%s'... failed!", Circles["red"], site.getName())
return text + "\n" + Circles["red"] + "Failed to stop site!" text += err.ToString()
} else {
text += fmt.Sprintf("%sStopping '%s'... success", Circles["green"], site.getName())
} }
return text + "\n" + Circles["green"] + "Successfully stopped site." }
return text
}, },
}, },
} }