From f63c90db2b834c6cfad984605d9c353230d10412 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Tue, 2 Feb 2021 18:37:26 -0600 Subject: [PATCH] Consolidate subcommand registration --- cmd/help.go | 17 +---- cmd/ls.go | 20 ------ cmd/main.go | 176 ++++++++++++++++++++++++++++++++++++++++++++++++ cmd/poweroff.go | 21 ------ cmd/poweron.go | 21 ------ cmd/setcolor.go | 41 ----------- cmd/setstate.go | 36 ---------- cmd/setwhite.go | 39 ----------- cmd/toggle.go | 23 ------- 9 files changed, 178 insertions(+), 216 deletions(-) diff --git a/cmd/help.go b/cmd/help.go index 64e97be..5814540 100644 --- a/cmd/help.go +++ b/cmd/help.go @@ -1,23 +1,10 @@ package lumecmd import ( - "flag" "fmt" + "sort" ) -func init() { - RegisterCommand("help", Command{ - 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() @@ -47,7 +34,7 @@ func printHelp(commands map[string]Command) { fmt.Println("\ncommands:") sort.Strings(keys) - for _, k = range keys { + for _, k := range keys { c := commands[k] fmt.Printf(" %-*s %s\n", maxLen, c.Name, c.Short) } diff --git a/cmd/ls.go b/cmd/ls.go index c05bdfb..f8cbeac 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -1,25 +1,5 @@ package lumecmd -import ( - "flag" -) - -func init() { - 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", - }) -} - 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 a85fe6d..ad497e4 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -11,6 +11,182 @@ import ( "github.com/BurntSushi/toml" ) +func init() { + 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("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", + }) +} + const lumercFile string = ".lumerc" func Main(args []string) (int, error) { diff --git a/cmd/poweroff.go b/cmd/poweroff.go index f990345..71e7a3b 100644 --- a/cmd/poweroff.go +++ b/cmd/poweroff.go @@ -1,30 +1,9 @@ package lumecmd import ( - "flag" - lifx "git.kill0.net/chill9/lume" ) -func init() { - 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", - }) -} - 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 c35a520..04eb81f 100644 --- a/cmd/poweron.go +++ b/cmd/poweron.go @@ -1,30 +1,9 @@ package lumecmd import ( - "flag" - lifx "git.kill0.net/chill9/lume" ) -func init() { - 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", - }) -} - 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 dfadce0..d936737 100644 --- a/cmd/setcolor.go +++ b/cmd/setcolor.go @@ -1,53 +1,12 @@ package lumecmd import ( - "flag" "fmt" "os" lifx "git.kill0.net/chill9/lume" ) -func init() { - 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", - }) -} - func SetColorCmd(args CmdArgs) (int, error) { c := args.Client state := lifx.State{} diff --git a/cmd/setstate.go b/cmd/setstate.go index a107871..bd71140 100644 --- a/cmd/setstate.go +++ b/cmd/setstate.go @@ -1,45 +1,9 @@ package lumecmd import ( - "flag" - lifx "git.kill0.net/chill9/lume" ) -func init() { - 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", - }) -} - func SetStateCmd(args CmdArgs) (int, error) { c := args.Client state := lifx.State{} diff --git a/cmd/setwhite.go b/cmd/setwhite.go index 8815aa2..3c19b28 100644 --- a/cmd/setwhite.go +++ b/cmd/setwhite.go @@ -1,48 +1,9 @@ package lumecmd import ( - "flag" - lifx "git.kill0.net/chill9/lume" ) -func init() { - 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", - }) -} - func SetWhiteCmd(args CmdArgs) (int, error) { c := args.Client state := lifx.State{} diff --git a/cmd/toggle.go b/cmd/toggle.go index 47c5332..daaafed 100644 --- a/cmd/toggle.go +++ b/cmd/toggle.go @@ -1,28 +1,5 @@ package lumecmd -import ( - "flag" -) - -func init() { - 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", - }) -} - func ToggleCmd(args CmdArgs) (int, error) { c := args.Client duration := args.Flags.Float64("duration")