go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/experiments/huectl/pkg/command/set_group_color.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package command
     9  
    10  import (
    11  	"github.com/urfave/cli/v2"
    12  
    13  	"go.charczuk.com/experiments/huectl/pkg/hue"
    14  )
    15  
    16  // SetGroupColor returns a command.
    17  func SetGroupColor() *cli.Command {
    18  	return &cli.Command{
    19  		Name:  "set-group-color",
    20  		Usage: "Set all of a given group's lights to a given color.",
    21  		Flags: append(DefaultFlags, append(GroupFlags,
    22  			&cli.StringFlag{
    23  				Name:    "color",
    24  				Aliases: []string{"c"},
    25  				Usage:   "The hex color to set the lights to (ex. #efefef)",
    26  			},
    27  			&cli.DurationFlag{
    28  				Name:  "transition",
    29  				Usage: "The transition time for the change in color",
    30  			})...,
    31  		),
    32  		Action: func(c *cli.Context) error {
    33  			group, _, err := groupHelper(c)
    34  			if err != nil {
    35  				return err
    36  			}
    37  
    38  			parsedColor := hue.ColorFromHex(c.String("color"))
    39  			if err = group.ColorTransition(c.Context, parsedColor, c.Duration("transition")); err != nil {
    40  				return err
    41  			}
    42  
    43  			printf(c, "group %q set to %s\n", group.Name, parsedColor.String())
    44  			return nil
    45  		},
    46  	}
    47  }