up3
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

This commit is contained in:
2026-07-09 08:33:57 +08:00
commit 26ed99fda6
845 changed files with 75419 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
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
}