2019-02-10 13:04:11 -05:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-01-02 20:26:48 -05:00
|
|
|
"log"
|
|
|
|
"os"
|
2019-02-10 13:04:11 -05:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Description struct {
|
|
|
|
Short string
|
|
|
|
Usage []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Command interface {
|
|
|
|
Name() string
|
|
|
|
Description() Description
|
|
|
|
Execute(args []string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
commandRegistry = make(map[string]Command)
|
2020-01-02 20:26:48 -05:00
|
|
|
ctllog = log.New(os.Stderr, "v2ctl> ", 0)
|
2019-02-10 13:04:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func RegisterCommand(cmd Command) error {
|
|
|
|
entry := strings.ToLower(cmd.Name())
|
2019-06-14 09:47:28 -04:00
|
|
|
if entry == "" {
|
2019-02-10 13:04:11 -05:00
|
|
|
return newError("empty command name")
|
|
|
|
}
|
|
|
|
commandRegistry[entry] = cmd
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetCommand(name string) Command {
|
|
|
|
cmd, found := commandRegistry[name]
|
|
|
|
if !found {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
type hiddenCommand interface {
|
|
|
|
Hidden() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintUsage() {
|
|
|
|
for name, cmd := range commandRegistry {
|
|
|
|
if _, ok := cmd.(hiddenCommand); ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Println(" ", name, "\t\t\t", cmd.Description())
|
|
|
|
}
|
|
|
|
}
|