Compare commits

...

2 Commits

Author SHA1 Message Date
f63c90db2b
Consolidate subcommand registration 2021-02-02 18:37:26 -06:00
1a99e03f88
Sort commands in help output
Closes #5
2021-02-02 18:34:37 -06:00
9 changed files with 184 additions and 217 deletions

View File

@ -1,23 +1,10 @@
package lumecmd package lumecmd
import ( import (
"flag"
"fmt" "fmt"
"sort"
) )
func init() {
RegisterCommand("help", Command{
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()
@ -32,7 +19,10 @@ func HelpCmd(args CmdArgs) (int, error) {
func printHelp(commands map[string]Command) { func printHelp(commands map[string]Command) {
var maxLen, cmdLen int var maxLen, cmdLen int
var keys []string
for _, c := range commands { for _, c := range commands {
keys = append(keys, c.Name)
cmdLen = len(c.Name) cmdLen = len(c.Name)
if cmdLen > maxLen { if cmdLen > maxLen {
maxLen = cmdLen maxLen = cmdLen
@ -41,9 +31,11 @@ func printHelp(commands map[string]Command) {
fmt.Printf("usage:\n lume <command> [<args...>]") fmt.Printf("usage:\n lume <command> [<args...>]")
fmt.Println() fmt.Println()
fmt.Println("\ncommands:") fmt.Println("\ncommands:")
for _, c := range commands {
sort.Strings(keys)
for _, k := range keys {
c := commands[k]
fmt.Printf(" %-*s %s\n", maxLen, c.Name, c.Short) fmt.Printf(" %-*s %s\n", maxLen, c.Name, c.Short)
} }
} }

View File

@ -1,25 +1,5 @@
package lumecmd 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=<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

@ -11,6 +11,182 @@ import (
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
) )
func init() {
RegisterCommand("help", Command{
Func: HelpCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("help", flag.ExitOnError)
return fs
}(),
Use: "<command>",
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=<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("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"
func Main(args []string) (int, error) { func Main(args []string) (int, error) {

View File

@ -1,30 +1,9 @@
package lumecmd package lumecmd
import ( import (
"flag"
lifx "git.kill0.net/chill9/lume" 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 <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,30 +1,9 @@
package lumecmd package lumecmd
import ( import (
"flag"
lifx "git.kill0.net/chill9/lume" 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 <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,53 +1,12 @@
package lumecmd package lumecmd
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
lifx "git.kill0.net/chill9/lume" 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 <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,45 +1,9 @@
package lumecmd package lumecmd
import ( import (
"flag"
lifx "git.kill0.net/chill9/lume" 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 <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,48 +1,9 @@
package lumecmd package lumecmd
import ( import (
"flag"
lifx "git.kill0.net/chill9/lume" 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 <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,28 +1,5 @@
package lumecmd 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 <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")