Files
mc/server/cmd/register.go
T
TarnaWijaya 26ed99fda6
Build and deploy / Build (push) Has been cancelled
Build and deploy / Update Contributors (push) Has been cancelled
Build and deploy / Deploy (push) Has been cancelled
up3
2026-07-09 08:33:57 +08:00

36 lines
1004 B
Go

package cmd
import "sync"
// commands holds a list of registered commands indexed by their name.
var commands sync.Map
// Register registers a command with its name and all aliases that it has. Any command with the same name or
// aliases will be overwritten.
func Register(command Command) {
commands.Store(command.name, command)
for _, alias := range command.aliases {
commands.Store(alias, command)
}
}
// ByAlias looks up a command by an alias. If found, the command and true are returned. If not, the returned
// command is nil and the bool is false.
func ByAlias(alias string) (Command, bool) {
command, ok := commands.Load(alias)
if !ok {
return Command{}, false
}
return command.(Command), ok
}
// Commands returns a map of all registered commands indexed by the alias they were registered with.
func Commands() map[string]Command {
cmd := make(map[string]Command)
commands.Range(func(key, value any) bool {
cmd[key.(string)] = value.(Command)
return true
})
return cmd
}