lume/cmd/command.go

55 lines
942 B
Go
Raw Normal View History

2020-03-25 00:12:35 +00:00
package lumecmd
import (
"flag"
2020-03-27 00:05:50 +00:00
"fmt"
2020-03-25 00:12:35 +00:00
"strconv"
"git.kill0.net/chill9/go-lifx"
)
type CmdArgs struct {
2020-03-27 00:05:50 +00:00
Flags Flags
2020-03-25 00:12:35 +00:00
Client *lifx.Client
Selector string
}
type Flags struct {
*flag.FlagSet
}
2020-03-27 00:05:50 +00:00
type Command struct {
2020-03-28 00:31:41 +00:00
Name string
2020-03-27 00:05:50 +00:00
Func func(CmdArgs) int
Flags *flag.FlagSet
}
var commandRegistry = make(map[string]Command)
2020-03-25 00:12:35 +00:00
func (f Flags) String(name string) string {
return f.FlagSet.Lookup(name).Value.String()
}
func (f Flags) Float64(name string) float64 {
val, _ := strconv.ParseFloat(f.String(name), 64)
return val
}
func (f Flags) Bool(name string) bool {
val, _ := strconv.ParseBool(f.String(name))
return val
}
2020-03-27 00:05:50 +00:00
func RegisterCommand(name string, cmd Command) error {
if _, ok := commandRegistry[name]; ok {
return fmt.Errorf("%s command is already registered")
}
commandRegistry[name] = cmd
return nil
}
func GetCommand(name string) (Command, bool) {
cmd, ok := commandRegistry[name]
return cmd, ok
}