go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/experiments/huectl/pkg/command/list_scenes.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  	"os"
    13  
    14  	"github.com/urfave/cli/v2"
    15  
    16  	"go.charczuk.com/experiments/huectl/pkg/hue"
    17  )
    18  
    19  // ListScenes returns a command.
    20  func ListScenes() *cli.Command {
    21  	return &cli.Command{
    22  		Name:  "list-scenes",
    23  		Usage: "List scenes associated with a given bridge.",
    24  		Flags: append(DefaultFlags, append(OutputFlags, GroupFlags...)...),
    25  		Action: func(c *cli.Context) error {
    26  			bridge, err := initHelper(c)
    27  			if err != nil {
    28  				return err
    29  			}
    30  
    31  			if c.Int(flagGroupID) > 0 && c.String(flagGroupName) != "" {
    32  				return fmt.Errorf("please specify one of --group-id or --group-name")
    33  			}
    34  			var group *hue.Group
    35  			if c.Int(flagGroupID) > 0 || c.String(flagGroupName) != "" {
    36  				group, err = getGroup(c, &bridge, getGroupArgs{
    37  					ID:   c.Int(flagGroupID),
    38  					Name: c.String(flagGroupName),
    39  				})
    40  				if err != nil {
    41  					return err
    42  				}
    43  			}
    44  
    45  			scenes, err := bridge.GetScenes(c.Context)
    46  			if err != nil {
    47  				return err
    48  			}
    49  
    50  			if group != nil {
    51  				groupID := fmt.Sprint(group.ID)
    52  				var filtered []hue.Scene
    53  				for _, scene := range scenes {
    54  					if scene.Group == groupID {
    55  						filtered = append(filtered, scene)
    56  					}
    57  				}
    58  				output(c, os.Stdout, filtered)
    59  				return nil
    60  			}
    61  			output(c, os.Stdout, scenes)
    62  			return nil
    63  		},
    64  	}
    65  }