2021-04-21 01:33:35 +00:00
|
|
|
package lumecmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2021-04-23 22:56:58 +00:00
|
|
|
"strings"
|
2021-04-21 01:33:35 +00:00
|
|
|
|
|
|
|
"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: "<command>",
|
|
|
|
Short: "Validate a color string",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ValidateCmd(ctx Context) (int, error) {
|
2021-04-23 22:56:58 +00:00
|
|
|
var b strings.Builder
|
2021-04-21 01:33:35 +00:00
|
|
|
c := ctx.Client
|
|
|
|
|
|
|
|
if len(ctx.Args) != 1 {
|
2021-04-23 22:56:58 +00:00
|
|
|
fmt.Print(printCmdHelp(ctx.Name))
|
2021-04-21 01:33:35 +00:00
|
|
|
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 {
|
2021-04-23 22:56:58 +00:00
|
|
|
fmt.Fprintln(&b, validColor)
|
2021-04-21 01:33:35 +00:00
|
|
|
} else {
|
|
|
|
return ExitFailure, errors.New("go type %T but wanted *HSBKColor")
|
|
|
|
}
|
2021-04-23 22:56:58 +00:00
|
|
|
|
|
|
|
fmt.Print(b.String())
|
2021-04-21 01:33:35 +00:00
|
|
|
|
|
|
|
return ExitSuccess, nil
|
|
|
|
}
|