48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package registry
|
|
|
|
import (
|
|
"sort"
|
|
|
|
"ops-assistant/internal/core/command"
|
|
)
|
|
|
|
type Handler func(userID int64, cmd *command.ParsedCommand) (string, error)
|
|
|
|
type Registry struct {
|
|
handlers map[string]Handler
|
|
moduleHandlers map[string]Handler
|
|
}
|
|
|
|
func New() *Registry {
|
|
return &Registry{handlers: map[string]Handler{}, moduleHandlers: map[string]Handler{}}
|
|
}
|
|
|
|
func (r *Registry) Register(name string, h Handler) {
|
|
r.handlers[name] = h
|
|
}
|
|
|
|
func (r *Registry) RegisterModule(module string, h Handler) {
|
|
r.moduleHandlers[module] = h
|
|
}
|
|
|
|
func (r *Registry) Handle(userID int64, cmd *command.ParsedCommand) (string, bool, error) {
|
|
if h, ok := r.handlers[cmd.Name]; ok {
|
|
out, err := h(userID, cmd)
|
|
return out, true, err
|
|
}
|
|
if h, ok := r.moduleHandlers[cmd.Module]; ok {
|
|
out, err := h(userID, cmd)
|
|
return out, true, err
|
|
}
|
|
return "", false, nil
|
|
}
|
|
|
|
func (r *Registry) ListModules() []string {
|
|
mods := make([]string, 0, len(r.moduleHandlers))
|
|
for m := range r.moduleHandlers {
|
|
mods = append(mods, m)
|
|
}
|
|
sort.Strings(mods)
|
|
return mods
|
|
}
|