LapisDeploy/matrix_commands.go

141 lines
4.6 KiB
Go
Raw Permalink Normal View History

package main
import (
"context"
"fmt"
"maunium.net/go/mautrix/event"
)
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 error
sites, err = getAllSites() // Get all sites
if err != nil { // 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{
{
cmd: "help",
description: "Show the help dialouge",
callback: func(ctx context.Context, evt *event.Event, args []string) string {
text := ""
for _, cmd := range Chatcommands {
text = fmt.Sprintf("%s- %s\n %s\n", text, cmd.cmd, cmd.description)
}
return text
},
},
{
cmd: "sites",
description: "Display all available sites",
callback: func(ctx context.Context, evt *event.Event, args []string) string {
2024-04-12 11:19:43 -05:00
text := Circles["white"] + "Getting sites...\n"
sites, err := getAllSites()
if err != nil {
2024-04-12 11:19:43 -05:00
return text + Circles["red"] + "Failed to get sites!"
}
for _, site := range sites {
text = fmt.Sprintf("%s%s", text, site.getName())
if site.config.name != "" { // Only add technical name to sites with a configured name.
text += " (" + site.name + ")"
}
text += "\n"
}
if len(sites) == 0 {
text = text + "*No sites found*"
}
return text
},
},
{
cmd: "restart",
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 no arguments were given
2024-04-12 11:19:43 -05:00
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"
}
for _, site := range sites { // For every site queued to be restarted,
if err := site.Restart(); err != nil { // Attempt to restart it
text += fmt.Sprintf("%sRestarting '%s'... failed!\n", Circles["red"], site.getName())
} else { // Otherwise, success.
text += fmt.Sprintf("%sRestarting '%s'... success", Circles["green"], site.getName())
}
}
return text // Return the message.
},
},
{
cmd: "start",
description: "Start a site.",
callback: func(ctx context.Context, evt *event.Event, args []string) string {
if len(args) < 2 {
2024-04-12 11:19:43 -05:00
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"
}
for _, site := range sites {
if err := site.Start(); err != nil {
text += fmt.Sprintf("%sStarting '%s'... failed!\n", Circles["red"], site.getName())
} else {
text += fmt.Sprintf("%sStarting '%s'... success!\n", Circles["green"], site.getName())
}
}
return text
},
},
{
cmd: "stop",
description: "Stop a site.",
callback: func(ctx context.Context, evt *event.Event, args []string) string {
if len(args) < 2 {
2024-04-12 11:19:43 -05:00
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"
}
for _, site := range sites {
if err := site.Stop(); err != nil {
text += fmt.Sprintf("%sStopping '%s'... failed: %s", Circles["red"], site.getName(), err)
} else {
text += fmt.Sprintf("%sStopping '%s'... success", Circles["green"], site.getName())
}
}
return text
},
},
}
}