LapisDeploy/matrix_commands.go

113 lines
3.2 KiB
Go

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 {
text := Circles["white"] + "Getting sites...\n"
sites, err := getAllSites()
if err.code != 0 {
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 {
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."
}
text := Circles["white"] + "Restarting server..."
derr := site.Restart()
if derr.code != 0 {
derr.SendOverMatrix()
return text + "\n" + Circles["red"] + "Failed to restart site!"
}
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 {
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."
}
text := Circles["white"] + "Starting server..."
if derr := site.Start(); derr.code != 0 {
derr.SendOverMatrix()
return text + "\n" + Circles["red"] + "Failed to stop site!"
}
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 {
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."
}
text := Circles["white"] + "Stopping server..."
if derr := site.Stop(); derr.code != 0 {
derr.SendOverMatrix()
return text + "\n" + Circles["red"] + "Failed to stop site!"
}
return text + "\n" + Circles["green"] + "Successfully stopped site."
},
},
}
}