47 lines
1 KiB
Go
47 lines
1 KiB
Go
|
|
package types
|
||
|
|
|
||
|
|
// Command represents a CLI command
|
||
|
|
type Command struct {
|
||
|
|
Name string
|
||
|
|
Description string
|
||
|
|
Handler func(args []string) error
|
||
|
|
}
|
||
|
|
|
||
|
|
// CommandRegistry manages available commands
|
||
|
|
type CommandRegistry struct {
|
||
|
|
commands map[string]*Command
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewCommandRegistry creates a new command registry
|
||
|
|
func NewCommandRegistry() *CommandRegistry {
|
||
|
|
return &CommandRegistry{
|
||
|
|
commands: make(map[string]*Command),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Register adds a command to the registry
|
||
|
|
func (r *CommandRegistry) Register(cmd *Command) {
|
||
|
|
r.commands[cmd.Name] = cmd
|
||
|
|
}
|
||
|
|
|
||
|
|
// Get retrieves a command by name
|
||
|
|
func (r *CommandRegistry) Get(name string) (*Command, bool) {
|
||
|
|
cmd, exists := r.commands[name]
|
||
|
|
return cmd, exists
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetAll returns all registered commands
|
||
|
|
func (r *CommandRegistry) GetAll() []*Command {
|
||
|
|
cmds := make([]*Command, 0, len(r.commands))
|
||
|
|
for _, cmd := range r.commands {
|
||
|
|
cmds = append(cmds, cmd)
|
||
|
|
}
|
||
|
|
return cmds
|
||
|
|
}
|
||
|
|
|
||
|
|
// Has checks if a command exists
|
||
|
|
func (r *CommandRegistry) Has(name string) bool {
|
||
|
|
_, exists := r.commands[name]
|
||
|
|
return exists
|
||
|
|
}
|