go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/experiments/huectl/pkg/command/set_group_brightness.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  	"fmt"
    12  
    13  	"github.com/urfave/cli/v2"
    14  )
    15  
    16  // SetGroupBrightness returns a command.
    17  func SetGroupBrightness() *cli.Command {
    18  	return &cli.Command{
    19  		Name:  "set-group-brightness",
    20  		Usage: "Set all of a given group's lights to a given brightness.",
    21  		Flags: append(DefaultFlags, append(GroupFlags,
    22  			&cli.StringFlag{
    23  				Name:    "brightness",
    24  				Aliases: []string{"b"},
    25  				Usage:   "The brightness value (between 0-255)",
    26  			})...,
    27  		),
    28  		Action: func(c *cli.Context) error {
    29  			group, _, err := groupHelper(c)
    30  			if err != nil {
    31  				return err
    32  			}
    33  
    34  			brightness := c.Int("brightness")
    35  			if brightness < 0 || brightness > 255 {
    36  				return fmt.Errorf("invalid brightness value; should be 0-255")
    37  			}
    38  			if err = group.Brightness(c.Context, uint8(brightness)); err != nil {
    39  				return err
    40  			}
    41  
    42  			printf(c, "group %q brightness set to %d\n", group.Name, brightness)
    43  			return nil
    44  		},
    45  	}
    46  }