Improve command loading

This commit is contained in:
Ryan Cavicchioni 2021-02-16 23:24:32 -06:00
parent dd0bf763a3
commit c9e4d9af80
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
11 changed files with 251 additions and 190 deletions

View File

@ -81,12 +81,11 @@ func (f Flags) Bool(name string) bool {
return val return val
} }
func RegisterCommand(name string, cmd Command) error { func RegisterCommand(cmd Command) error {
if _, ok := commandRegistry[name]; ok { if _, ok := commandRegistry[cmd.Name]; ok {
return fmt.Errorf("%s command is already registered") return fmt.Errorf("%s command is already registered")
} }
cmd.Name = name commandRegistry[cmd.Name] = cmd
commandRegistry[name] = cmd
return nil return nil
} }

View File

@ -1,10 +1,25 @@
package lumecmd package lumecmd
import ( import (
"flag"
"fmt" "fmt"
"sort" "sort"
) )
func NewCmdHelp() Command {
return Command{
Name: "help",
Func: HelpCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("help", flag.ExitOnError)
return fs
}(),
Use: "<command>",
Short: "Show help for a command",
}
}
func HelpCmd(args CmdArgs) (int, error) { func HelpCmd(args CmdArgs) (int, error) {
argv := args.Flags.Args() argv := args.Flags.Args()

View File

@ -1,5 +1,24 @@
package lumecmd package lumecmd
import "flag"
func NewCmdLs() Command {
return Command{
Name: "ls",
Func: LsCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("ls", flag.ExitOnError)
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
return fs
}(),
Use: "[--selector=<selector>]",
Short: "List the lights",
}
}
func LsCmd(args CmdArgs) (int, error) { func LsCmd(args CmdArgs) (int, error) {
c := args.Client c := args.Client
selector := args.Flags.String("selector") selector := args.Flags.String("selector")

View File

@ -17,192 +17,15 @@ var userAgent string
func init() { func init() {
userAgent = initUserAgent() userAgent = initUserAgent()
RegisterCommand("help", Command{ RegisterCommand(NewCmdHelp())
Func: HelpCmd, RegisterCommand(NewCmdLs())
Flags: func() *flag.FlagSet { RegisterCommand(NewCmdPoweroff())
fs := flag.NewFlagSet("help", flag.ExitOnError) RegisterCommand(NewCmdPoweron())
RegisterCommand(NewCmdSetColor())
return fs RegisterCommand(NewCmdSetState())
}(), RegisterCommand(NewCmdSetWhite())
Use: "<command>", RegisterCommand(NewCmdShow())
Short: "Show help for a command", RegisterCommand(NewCmdToggle())
})
RegisterCommand("ls", Command{
Func: LsCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("ls", flag.ExitOnError)
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
return fs
}(),
Use: "[--selector=<selector>]",
Short: "List the lights",
})
RegisterCommand("poweroff", Command{
Func: PoweroffCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("poweroff", flag.ExitOnError)
duration := fs.Float64("duration", defaultDuration, "Set the duration")
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
return fs
}(),
Use: "[--selector <selector>] [--duration <sec>]",
Short: "Power on",
})
RegisterCommand("poweron", Command{
Func: PoweronCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("poweron", flag.ExitOnError)
duration := fs.Float64("duration", defaultDuration, "Set the duration")
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
return fs
}(),
Use: "[--selector <selector>] [--duration <sec>]",
Short: "Power on",
})
RegisterCommand("set-color", Command{
Func: SetColorCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("set-color", flag.ExitOnError)
selector := fs.String("selector", "all", "the selector")
fs.StringVar(selector, "s", "all", "the selector")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
hue := fs.String("hue", defaultHue, "hue level")
fs.StringVar(hue, "H", defaultHue, "hue level")
saturation := fs.String("saturation", defaultSaturation, "saturation level")
fs.StringVar(saturation, "S", defaultSaturation, "saturation level")
rgb := fs.String("rgb", defaultRGB, "RGB value")
fs.StringVar(rgb, "r", defaultRGB, "RGB value")
name := fs.String("name", defaultName, "named color")
fs.StringVar(name, "n", defaultName, "named color")
brightness := fs.String("brightness", defaultBrightness, "brightness state")
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
duration := fs.Float64("duration", defaultDuration, "duration state")
fs.Float64Var(duration, "d", defaultDuration, "duration state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
return fs
}(),
Use: "[--selector <selector>] [--power (on|off)] [--hue <hue>] [--saturation <saturation>] [--rgb <rbg>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--fast]",
Short: "Set the color",
})
RegisterCommand("set-state", Command{
Func: SetStateCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("set-state", flag.ExitOnError)
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
color := fs.String("color", defaultColor, "color state")
fs.StringVar(color, "c", defaultColor, "color state")
brightness := fs.String("brightness", defaultBrightness, "brightness state")
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
duration := fs.Float64("duration", defaultDuration, "duration state")
fs.Float64Var(duration, "d", defaultDuration, "duration state")
infrared := fs.String("infrared", defaultInfrared, "infrared state")
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
return fs
}(),
Use: "[--selector <selector>] [--power (on|off)] [--color <color>] [--brightness <brightness>] [--duration <sec>] [--infrared <infrared>] [--fast]",
Short: "Set various state attributes",
})
RegisterCommand("set-white", Command{
Func: SetWhiteCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("set-white", flag.ExitOnError)
selector := fs.String("selector", "all", "the selector")
fs.StringVar(selector, "s", "all", "the selector")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
kelvin := fs.String("kelvin", defaultWhiteKelvin, "kelvin level")
fs.StringVar(kelvin, "k", defaultWhiteKelvin, "kelvin level")
name := fs.String("name", defaultWhiteName, "named white level")
fs.StringVar(name, "n", defaultWhiteName, "named white level")
brightness := fs.String("brightness", defaultBrightness, "brightness state")
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
duration := fs.Float64("duration", defaultDuration, "duration state")
fs.Float64Var(duration, "d", defaultDuration, "duration state")
infrared := fs.String("infrared", defaultInfrared, "infrared state")
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
return fs
}(),
Use: "[--selector <selector>] [--power (on|off)] [--kelvin <kelvin>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--infrared] [--fast]",
Short: "Set the white level",
})
RegisterCommand("show", Command{
Func: ShowCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("show", flag.ExitOnError)
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
return fs
}(),
Use: "[--selector=<selector>]",
Short: "Show details about the lights",
})
RegisterCommand("toggle", Command{
Func: ToggleCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("toggle", flag.ExitOnError)
duration := fs.Float64("duration", defaultDuration, "Set the duration")
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
return fs
}(),
Use: "[--selector <selector>] [--duration <sec>]",
Short: "Toggle the power on/off",
})
} }
const lumercFile string = ".lumerc" const lumercFile string = ".lumerc"

View File

@ -1,9 +1,31 @@
package lumecmd package lumecmd
import ( import (
"flag"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
func NewCmdPoweroff() Command {
return Command{
Name: "poweroff",
Func: PoweroffCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("poweroff", flag.ExitOnError)
duration := fs.Float64("duration", defaultDuration, "Set the duration")
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
return fs
}(),
Use: "[--selector <selector>] [--duration <sec>]",
Short: "Power on",
}
}
func PoweroffCmd(args CmdArgs) (int, error) { func PoweroffCmd(args CmdArgs) (int, error) {
c := args.Client c := args.Client
duration := args.Flags.Float64("duration") duration := args.Flags.Float64("duration")

View File

@ -1,9 +1,31 @@
package lumecmd package lumecmd
import ( import (
"flag"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
func NewCmdPoweron() Command {
return Command{
Name: "poweron",
Func: PoweronCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("poweron", flag.ExitOnError)
duration := fs.Float64("duration", defaultDuration, "Set the duration")
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
return fs
}(),
Use: "[--selector <selector>] [--duration <sec>]",
Short: "Power on",
}
}
func PoweronCmd(args CmdArgs) (int, error) { func PoweronCmd(args CmdArgs) (int, error) {
c := args.Client c := args.Client
duration := args.Flags.Float64("duration") duration := args.Flags.Float64("duration")

View File

@ -1,11 +1,53 @@
package lumecmd package lumecmd
import ( import (
"flag"
"fmt" "fmt"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
func NewCmdSetColor() Command {
return Command{
Name: "set-color",
Func: SetColorCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("set-color", flag.ExitOnError)
selector := fs.String("selector", "all", "the selector")
fs.StringVar(selector, "s", "all", "the selector")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
hue := fs.String("hue", defaultHue, "hue level")
fs.StringVar(hue, "H", defaultHue, "hue level")
saturation := fs.String("saturation", defaultSaturation, "saturation level")
fs.StringVar(saturation, "S", defaultSaturation, "saturation level")
rgb := fs.String("rgb", defaultRGB, "RGB value")
fs.StringVar(rgb, "r", defaultRGB, "RGB value")
name := fs.String("name", defaultName, "named color")
fs.StringVar(name, "n", defaultName, "named color")
brightness := fs.String("brightness", defaultBrightness, "brightness state")
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
duration := fs.Float64("duration", defaultDuration, "duration state")
fs.Float64Var(duration, "d", defaultDuration, "duration state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
return fs
}(),
Use: "[--selector <selector>] [--power (on|off)] [--hue <hue>] [--saturation <saturation>] [--rgb <rbg>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--fast]",
Short: "Set the color",
}
}
func SetColorCmd(args CmdArgs) (int, error) { func SetColorCmd(args CmdArgs) (int, error) {
c := args.Client c := args.Client
state := lifx.State{} state := lifx.State{}

View File

@ -1,9 +1,46 @@
package lumecmd package lumecmd
import ( import (
"flag"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
func NewCmdSetState() Command {
return Command{
Name: "set-state",
Func: SetStateCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("set-state", flag.ExitOnError)
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
color := fs.String("color", defaultColor, "color state")
fs.StringVar(color, "c", defaultColor, "color state")
brightness := fs.String("brightness", defaultBrightness, "brightness state")
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
duration := fs.Float64("duration", defaultDuration, "duration state")
fs.Float64Var(duration, "d", defaultDuration, "duration state")
infrared := fs.String("infrared", defaultInfrared, "infrared state")
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
return fs
}(),
Use: "[--selector <selector>] [--power (on|off)] [--color <color>] [--brightness <brightness>] [--duration <sec>] [--infrared <infrared>] [--fast]",
Short: "Set various state attributes",
}
}
func SetStateCmd(args CmdArgs) (int, error) { func SetStateCmd(args CmdArgs) (int, error) {
c := args.Client c := args.Client
state := lifx.State{} state := lifx.State{}

View File

@ -1,9 +1,49 @@
package lumecmd package lumecmd
import ( import (
"flag"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
func NewCmdSetWhite() Command {
return Command{
Name: "set-white",
Func: SetWhiteCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("set-white", flag.ExitOnError)
selector := fs.String("selector", "all", "the selector")
fs.StringVar(selector, "s", "all", "the selector")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
kelvin := fs.String("kelvin", defaultWhiteKelvin, "kelvin level")
fs.StringVar(kelvin, "k", defaultWhiteKelvin, "kelvin level")
name := fs.String("name", defaultWhiteName, "named white level")
fs.StringVar(name, "n", defaultWhiteName, "named white level")
brightness := fs.String("brightness", defaultBrightness, "brightness state")
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
duration := fs.Float64("duration", defaultDuration, "duration state")
fs.Float64Var(duration, "d", defaultDuration, "duration state")
infrared := fs.String("infrared", defaultInfrared, "infrared state")
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
return fs
}(),
Use: "[--selector <selector>] [--power (on|off)] [--kelvin <kelvin>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--infrared] [--fast]",
Short: "Set the white level",
}
}
func SetWhiteCmd(args CmdArgs) (int, error) { func SetWhiteCmd(args CmdArgs) (int, error) {
c := args.Client c := args.Client
state := lifx.State{} state := lifx.State{}

View File

@ -1,11 +1,29 @@
package lumecmd package lumecmd
import ( import (
"flag"
"fmt" "fmt"
) )
const Tabstop int = 2 const Tabstop int = 2
func NewCmdShow() Command {
return Command{
Name: "show",
Func: ShowCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("show", flag.ExitOnError)
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
return fs
}(),
Use: "[--selector=<selector>]",
Short: "Show details about the lights",
}
}
func ShowCmd(args CmdArgs) (int, error) { func ShowCmd(args CmdArgs) (int, error) {
var indent int var indent int
c := args.Client c := args.Client

View File

@ -1,5 +1,29 @@
package lumecmd package lumecmd
import (
"flag"
)
func NewCmdToggle() Command {
return Command{
Name: "toggle",
Func: ToggleCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("toggle", flag.ExitOnError)
duration := fs.Float64("duration", defaultDuration, "Set the duration")
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
return fs
}(),
Use: "[--selector <selector>] [--duration <sec>]",
Short: "Toggle the power on/off",
}
}
func ToggleCmd(args CmdArgs) (int, error) { func ToggleCmd(args CmdArgs) (int, error) {
c := args.Client c := args.Client
duration := args.Flags.Float64("duration") duration := args.Flags.Float64("duration")