lume/cmd/validate.go

52 lines
871 B
Go
Raw Normal View History

2021-04-21 01:33:35 +00:00
package lumecmd
import (
"errors"
"flag"
"fmt"
"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) {
var b strings.Builder
2021-04-21 01:33:35 +00:00
c := ctx.Client
if len(ctx.Args) != 1 {
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 {
fmt.Fprintln(&b, validColor)
2021-04-21 01:33:35 +00:00
} else {
return ExitFailure, errors.New("go type %T but wanted *HSBKColor")
}
fmt.Print(b.String())
2021-04-21 01:33:35 +00:00
return ExitSuccess, nil
}