Compare commits

...

2 Commits

Author SHA1 Message Date
c9e4d9af80
Improve command loading 2021-02-16 23:24:32 -06:00
dd0bf763a3
Add PrintfWithIndent function 2021-02-16 20:20:18 -06:00
12 changed files with 284 additions and 209 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,8 +1,31 @@
package lumecmd package lumecmd
import "fmt" 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=<selector>]",
Short: "Show details about the lights",
}
}
func ShowCmd(args CmdArgs) (int, error) { func ShowCmd(args CmdArgs) (int, error) {
var indent int
c := args.Client c := args.Client
selector := args.Flags.String("selector") selector := args.Flags.String("selector")
lights, err := c.ListLights(selector) lights, err := c.ListLights(selector)
@ -14,24 +37,26 @@ func ShowCmd(args CmdArgs) (int, error) {
sortLights(lights) sortLights(lights)
for i, l := range lights { for i, l := range lights {
indent = 0
fmt.Printf( fmt.Printf(
"Light ID: %s, %s, Power: %s\n", "Light ID: %s, %s, Power: %s\n",
l.Id, l.Id,
connected(l.Connected), connected(l.Connected),
powerColor(l.Power), powerColor(l.Power),
) )
fmt.Printf(" Label: %s, ID: %s\n", l.Label, l.Id) indent += Tabstop
fmt.Printf(" UUID: %s\n", l.UUID) PrintfWithIndent(indent, "Label: %s, ID: %s\n", l.Label, l.Id)
fmt.Printf(" Location: %s, ID: %s\n", l.Location.Name, l.Location.Id) PrintfWithIndent(indent, "UUID: %s\n", l.UUID)
fmt.Printf(" Group: %s, ID: %s\n", l.Group.Name, l.Group.Id) PrintfWithIndent(indent, "Location: %s, ID: %s\n", l.Location.Name, l.Location.Id)
fmt.Printf(" Color: Hue: %.1f, Saturation: %.1f%%, Kelvin: %d\n", PrintfWithIndent(indent, "Group: %s, ID: %s\n", l.Group.Name, l.Group.Id)
PrintfWithIndent(indent, "Color: Hue: %.1f, Saturation: %.1f%%, Kelvin: %d\n",
*l.Color.H, *l.Color.S, *l.Color.K) *l.Color.H, *l.Color.S, *l.Color.K)
fmt.Printf(" Brightness: %.1f%%\n", l.Brightness*100) PrintfWithIndent(indent, "Brightness: %.1f%%\n", l.Brightness*100)
if l.Effect != "" { if l.Effect != "" {
fmt.Printf(" Effect: %s\n", l.Effect) PrintfWithIndent(indent, "Effect: %s\n", l.Effect)
} }
fmt.Printf(" Product: %s\n", l.Product.Name) PrintfWithIndent(indent, "Product: %s\n", l.Product.Name)
fmt.Printf(" Capabilities: ") PrintfWithIndent(indent, "Capabilities: ")
fmt.Printf("Color: %s, ", YesNo(l.Product.Capabilities.HasColor)) fmt.Printf("Color: %s, ", YesNo(l.Product.Capabilities.HasColor))
fmt.Printf("Variable Color Temp: %s, ", YesNo(l.Product.Capabilities.HasVariableColorTemp)) fmt.Printf("Variable Color Temp: %s, ", YesNo(l.Product.Capabilities.HasVariableColorTemp))
fmt.Printf("IR: %s, ", YesNo(l.Product.Capabilities.HasIR)) fmt.Printf("IR: %s, ", YesNo(l.Product.Capabilities.HasIR))
@ -41,14 +66,16 @@ func ShowCmd(args CmdArgs) (int, error) {
fmt.Printf("Max Kelvin: %.1f ", l.Product.Capabilities.MaxKelvin) fmt.Printf("Max Kelvin: %.1f ", l.Product.Capabilities.MaxKelvin)
fmt.Println() fmt.Println()
// List applicable selectors (most to least specific) // List applicable selectors (most to least specific)
fmt.Printf(" Selectors:\n") PrintfWithIndent(indent, "Selectors:\n")
fmt.Printf(" id:%s\n", l.Id) indent += Tabstop
fmt.Printf(" label:%s\n", l.Label) PrintfWithIndent(indent, "id:%s\n", l.Id)
fmt.Printf(" group_id:%s\n", l.Group.Id) PrintfWithIndent(indent, "label:%s\n", l.Label)
fmt.Printf(" group:%s\n", l.Group.Name) PrintfWithIndent(indent, "group_id:%s\n", l.Group.Id)
fmt.Printf(" location_id:%s\n", l.Location.Id) PrintfWithIndent(indent, "group:%s\n", l.Group.Name)
fmt.Printf(" location:%s\n", l.Location.Name) PrintfWithIndent(indent, "location_id:%s\n", l.Location.Id)
fmt.Printf(" Last Seen: %s (%.1fs ago)\n", l.LastSeen, l.SecondsLastSeen) PrintfWithIndent(indent, "location:%s\n", l.Location.Name)
indent -= Tabstop
PrintfWithIndent(indent, "Last Seen: %s (%.1fs ago)\n", l.LastSeen, l.SecondsLastSeen)
if i < len(lights)-1 { if i < len(lights)-1 {
fmt.Println() fmt.Println()

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")

View File

@ -169,4 +169,9 @@ func YesNo(v bool) string {
func PrintWithIndent(indent int, s string) { func PrintWithIndent(indent int, s string) {
fmt.Printf("%*s%s", indent, "", s) fmt.Printf("%*s%s", indent, "", s)
} }
func PrintfWithIndent(indent int, format string, a ...interface{}) (n int, err error) {
format = fmt.Sprintf("%*s%s", indent, "", format)
return fmt.Printf(format, a...)
}