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