LapisDeploy/matrix_commands.go

113 lines
3.2 KiB
Go
Raw Normal View History

package main
import (
"context"
"fmt"
"maunium.net/go/mautrix/event"
)
var Chatcommands = []MatrixCommand{}
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.code != 0 {
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 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."
}
site, found, err := getSite(args[1])
if err != nil {
2024-04-12 11:19:43 -05:00
return Circles["red"] + "Failed to get site " + args[2] + "!"
} else if !found {
2024-04-12 11:19:43 -05:00
return Circles["red"] + "Invalid site."
}
2024-04-12 11:19:43 -05:00
text := Circles["white"] + "Restarting server..."
derr := site.Restart()
if derr.code != 0 {
derr.SendOverMatrix()
2024-04-12 11:19:43 -05:00
return text + "\n" + Circles["red"] + "Failed to restart site!"
}
2024-04-12 11:19:43 -05:00
return text + "\n" + Circles["green"] + "Successfully restarted site."
},
},
{
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."
}
site, found, err := getSite(args[1])
if err != nil {
2024-04-12 11:19:43 -05:00
return Circles["red"] + "Failed to get site " + args[2] + "!"
} else if !found {
2024-04-12 11:19:43 -05:00
return Circles["red"] + "Invalid site."
}
2024-04-12 11:19:43 -05:00
text := Circles["white"] + "Starting server..."
if derr := site.Start(); derr.code != 0 {
derr.SendOverMatrix()
2024-04-12 11:19:43 -05:00
return text + "\n" + Circles["red"] + "Failed to stop site!"
}
2024-04-12 11:19:43 -05:00
return text + "\n" + Circles["green"] + "Successfully started site."
},
},
{
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."
}
site, found, err := getSite(args[1])
if err != nil {
2024-04-12 11:19:43 -05:00
return Circles["red"] + "Failed to get site " + args[2] + "!"
} else if !found {
2024-04-12 11:19:43 -05:00
return Circles["red"] + "Invalid site."
}
2024-04-12 11:19:43 -05:00
text := Circles["white"] + "Stopping server..."
if derr := site.Stop(); derr.code != 0 {
derr.SendOverMatrix()
2024-04-12 11:19:43 -05:00
return text + "\n" + Circles["red"] + "Failed to stop site!"
}
2024-04-12 11:19:43 -05:00
return text + "\n" + Circles["green"] + "Successfully stopped site."
},
},
}
}