diff --git a/cmd/command.go b/cmd/command.go index 7d13268..82a652c 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -81,12 +81,11 @@ func (f Flags) Bool(name string) bool { return val } -func RegisterCommand(name string, cmd Command) error { - if _, ok := commandRegistry[name]; ok { +func RegisterCommand(cmd Command) error { + if _, ok := commandRegistry[cmd.Name]; ok { return fmt.Errorf("%s command is already registered") } - cmd.Name = name - commandRegistry[name] = cmd + commandRegistry[cmd.Name] = cmd return nil } diff --git a/cmd/help.go b/cmd/help.go index 5814540..ee16004 100644 --- a/cmd/help.go +++ b/cmd/help.go @@ -1,10 +1,25 @@ package lumecmd import ( + "flag" "fmt" "sort" ) +func NewCmdHelp() Command { + return Command{ + Name: "help", + Func: HelpCmd, + Flags: func() *flag.FlagSet { + fs := flag.NewFlagSet("help", flag.ExitOnError) + + return fs + }(), + Use: "", + Short: "Show help for a command", + } +} + func HelpCmd(args CmdArgs) (int, error) { argv := args.Flags.Args() diff --git a/cmd/ls.go b/cmd/ls.go index f8cbeac..09f9d44 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -1,5 +1,24 @@ 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=]", + Short: "List the lights", + } +} + func LsCmd(args CmdArgs) (int, error) { c := args.Client selector := args.Flags.String("selector") diff --git a/cmd/main.go b/cmd/main.go index 300e95b..e4928d3 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,192 +17,15 @@ var userAgent string func init() { userAgent = initUserAgent() - RegisterCommand("help", Command{ - Func: HelpCmd, - Flags: func() *flag.FlagSet { - fs := flag.NewFlagSet("help", flag.ExitOnError) - - return fs - }(), - Use: "", - Short: "Show help for a command", - }) - 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=]", - 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 ] [--duration ]", - 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 ] [--duration ]", - 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 ] [--power (on|off)] [--hue ] [--saturation ] [--rgb ] [--name ] [--brightness ] [--duration ] [--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 ] [--power (on|off)] [--color ] [--brightness ] [--duration ] [--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 ] [--power (on|off)] [--kelvin ] [--name ] [--brightness ] [--duration ] [--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=]", - 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 ] [--duration ]", - Short: "Toggle the power on/off", - }) + RegisterCommand(NewCmdHelp()) + RegisterCommand(NewCmdLs()) + RegisterCommand(NewCmdPoweroff()) + RegisterCommand(NewCmdPoweron()) + RegisterCommand(NewCmdSetColor()) + RegisterCommand(NewCmdSetState()) + RegisterCommand(NewCmdSetWhite()) + RegisterCommand(NewCmdShow()) + RegisterCommand(NewCmdToggle()) } const lumercFile string = ".lumerc" diff --git a/cmd/poweroff.go b/cmd/poweroff.go index 35b8be8..c99ff0a 100644 --- a/cmd/poweroff.go +++ b/cmd/poweroff.go @@ -1,9 +1,31 @@ package lumecmd import ( + "flag" + "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 ] [--duration ]", + Short: "Power on", + } +} + func PoweroffCmd(args CmdArgs) (int, error) { c := args.Client duration := args.Flags.Float64("duration") diff --git a/cmd/poweron.go b/cmd/poweron.go index d4d9c02..5a509fe 100644 --- a/cmd/poweron.go +++ b/cmd/poweron.go @@ -1,9 +1,31 @@ package lumecmd import ( + "flag" + "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 ] [--duration ]", + Short: "Power on", + } +} + func PoweronCmd(args CmdArgs) (int, error) { c := args.Client duration := args.Flags.Float64("duration") diff --git a/cmd/setcolor.go b/cmd/setcolor.go index 25d2a4c..eb6037d 100644 --- a/cmd/setcolor.go +++ b/cmd/setcolor.go @@ -1,11 +1,53 @@ package lumecmd import ( + "flag" "fmt" "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 ] [--power (on|off)] [--hue ] [--saturation ] [--rgb ] [--name ] [--brightness ] [--duration ] [--fast]", + Short: "Set the color", + } +} + func SetColorCmd(args CmdArgs) (int, error) { c := args.Client state := lifx.State{} diff --git a/cmd/setstate.go b/cmd/setstate.go index a4e831c..9886978 100644 --- a/cmd/setstate.go +++ b/cmd/setstate.go @@ -1,9 +1,46 @@ package lumecmd import ( + "flag" + "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 ] [--power (on|off)] [--color ] [--brightness ] [--duration ] [--infrared ] [--fast]", + Short: "Set various state attributes", + } +} + func SetStateCmd(args CmdArgs) (int, error) { c := args.Client state := lifx.State{} diff --git a/cmd/setwhite.go b/cmd/setwhite.go index 30c881c..4f8109b 100644 --- a/cmd/setwhite.go +++ b/cmd/setwhite.go @@ -1,9 +1,49 @@ package lumecmd import ( + "flag" + "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 ] [--power (on|off)] [--kelvin ] [--name ] [--brightness ] [--duration ] [--infrared] [--fast]", + Short: "Set the white level", + } +} + func SetWhiteCmd(args CmdArgs) (int, error) { c := args.Client state := lifx.State{} diff --git a/cmd/show.go b/cmd/show.go index ba3a339..e42ec03 100644 --- a/cmd/show.go +++ b/cmd/show.go @@ -1,11 +1,29 @@ package lumecmd import ( + "flag" "fmt" ) 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=]", + Short: "Show details about the lights", + } +} + func ShowCmd(args CmdArgs) (int, error) { var indent int c := args.Client diff --git a/cmd/toggle.go b/cmd/toggle.go index daaafed..01151aa 100644 --- a/cmd/toggle.go +++ b/cmd/toggle.go @@ -1,5 +1,29 @@ 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 ] [--duration ]", + Short: "Toggle the power on/off", + } +} + func ToggleCmd(args CmdArgs) (int, error) { c := args.Client duration := args.Flags.Float64("duration")