zutils/pkg/types/registry.go

40 lines
770 B
Go
Raw Permalink Normal View History

package types
type Command struct {
Name string
Description string
Handler func(args []string) error
}
type CommandRegistry struct {
commands map[string]*Command
}
func NewCommandRegistry() *CommandRegistry {
return &CommandRegistry{
commands: make(map[string]*Command),
}
}
func (r *CommandRegistry) Register(cmd *Command) {
r.commands[cmd.Name] = cmd
}
func (r *CommandRegistry) Get(name string) (*Command, bool) {
cmd, exists := r.commands[name]
return cmd, exists
}
func (r *CommandRegistry) GetAll() []*Command {
cmds := make([]*Command, 0, len(r.commands))
for _, cmd := range r.commands {
cmds = append(cmds, cmd)
}
return cmds
}
func (r *CommandRegistry) Has(name string) bool {
_, exists := r.commands[name]
return exists
}