From 23210af0c3277b9beda64f606ddcf1e57db4b8bd Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Wed, 21 Apr 2021 01:33:35 +0000 Subject: [PATCH] Add a command for validateColor API --- cmd/main.go | 1 + cmd/validate.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ lume.1 | 4 ++++ 3 files changed, 53 insertions(+) create mode 100644 cmd/validate.go diff --git a/cmd/main.go b/cmd/main.go index 5de8643..40d8f29 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -20,6 +20,7 @@ func init() { RegisterCommand(NewCmdToggle()) RegisterCommand(NewCmdVersion()) RegisterCommand(NewCmdBreathe()) + RegisterCommand(NewCmdValidate()) } var Version string diff --git a/cmd/validate.go b/cmd/validate.go new file mode 100644 index 0000000..5eefb19 --- /dev/null +++ b/cmd/validate.go @@ -0,0 +1,48 @@ +package lumecmd + +import ( + "errors" + "flag" + "fmt" + + "git.kill0.net/chill9/lifx-go" +) + +func NewCmdValidate() Command { + return Command{ + Name: "validate", + Func: ValidateCmd, + Flags: func() *flag.FlagSet { + fs := flag.NewFlagSet("validate", flag.ExitOnError) + + return fs + }(), + Use: "", + Short: "Validate a color string", + } +} + +func ValidateCmd(ctx Context) (int, error) { + c := ctx.Client + + if len(ctx.Args) != 1 { + printCmdHelp(ctx.Name) + return ExitFailure, nil + } + + color := lifx.NamedColor(ctx.Args[0]) + + i, err := c.ValidateColor(color) + if err != nil { + return ExitFailure, err + } + + if validColor, ok := i.(*lifx.HSBKColor); ok { + fmt.Print(validColor) + } else { + return ExitFailure, errors.New("go type %T but wanted *HSBKColor") + } + fmt.Println() + + return ExitSuccess, nil +} diff --git a/lume.1 b/lume.1 index d4437c2..f0c5613 100644 --- a/lume.1 +++ b/lume.1 @@ -90,6 +90,10 @@ Show extended details about the lights .Op Fl Fl simple | Fl Fl table .Xc Toggle the power +.It Xo Ic validate +.Ar color_string +.Xc +Validate a color string .It Xo Ic version .Xc Print the version