Merge branch 'ordered-commands' into 'main'

Use a command list (not a map) to have a consistent manually-defined commands order

See merge request etke.cc/postmoogle!10
This commit is contained in:
Aine
2022-08-24 09:05:54 +00:00
2 changed files with 39 additions and 18 deletions

View File

@@ -16,22 +16,16 @@ import (
type sanitizerFunc func(string) string
var (
commands = map[string]string{
commands = utils.List{
// special commands
"help": "Get help",
"stop": "Disable bridge for the room and clear all configuration",
{K: "help", V: "Show this help message"},
{K: "stop", V: "Disable bridge for the room and clear all configuration"},
// options commands
optionMailbox: "Get or set mailbox of the room",
optionOwner: "Get or set owner of the room",
optionNoSender: fmt.Sprintf(
"Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)",
optionNoSender,
),
optionNoSubject: fmt.Sprintf(
"Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)",
optionNoSubject,
),
{K: optionMailbox, V: "Get or set mailbox of the room"},
{K: optionOwner, V: "Get or set owner of the room"},
{K: optionNoSender, V: fmt.Sprintf("Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)", optionNoSender)},
{K: optionNoSubject, V: fmt.Sprintf("Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)", optionNoSubject)},
}
// sanitizers is map of option name => sanitizer function
@@ -43,7 +37,7 @@ var (
)
func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []string) {
if _, ok := commands[command[0]]; !ok {
if _, ok := commands.Get(command[0]); !ok {
return
}
@@ -82,13 +76,13 @@ func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
var msg strings.Builder
msg.WriteString("the following commands are supported:\n\n")
for name, desc := range commands {
commands.ForEach(func(command, description string) {
msg.WriteString("* **")
msg.WriteString(name)
msg.WriteString(command)
msg.WriteString("** - ")
msg.WriteString(desc)
msg.WriteString(description)
msg.WriteString("\n")
}
})
content := format.RenderMarkdown(msg.String(), true, true)
content.MsgType = event.MsgNotice

27
utils/list.go Normal file
View File

@@ -0,0 +1,27 @@
package utils
// ListItem with key and value
type ListItem struct {
K string
V string
}
// List slice
type List []ListItem
// Get item's value
func (l List) Get(key string) (string, bool) {
for _, item := range l {
if item.K == key {
return item.V, true
}
}
return "", false
}
// ForEach item
func (l List) ForEach(handler func(key, value string)) {
for _, item := range l {
handler(item.K, item.V)
}
}