ziggs/internal/cli/create.go

78 lines
1.7 KiB
Go
Raw Normal View History

2022-09-26 15:25:21 +00:00
package cli
import (
"errors"
"strconv"
2023-05-29 01:08:52 +00:00
"strings"
2022-09-26 15:25:21 +00:00
2023-05-29 01:08:52 +00:00
"github.com/yunginnanet/huego"
2022-09-26 15:25:21 +00:00
"git.tcp.direct/kayos/ziggs/internal/ziggy"
)
func cmdCreate(br *ziggy.Bridge, args []string) error {
if len(args) < 2 {
return errors.New("not enough arguments")
}
switch args[0] {
case "group":
2022-10-14 06:38:13 +00:00
var (
name = args[1]
ids []string
groupType = "LightGroup"
class = ""
)
2023-05-29 01:08:52 +00:00
log.Debug().Msgf("creating group: %s", name)
for i, arg := range args {
2022-10-14 06:38:13 +00:00
switch arg {
case "group", name:
continue
case "-entertainment":
groupType = "Entertainment"
class = "Other"
2023-05-29 01:08:52 +00:00
log.Debug().Msgf("group type: %s", groupType)
log.Debug().Msgf("group class: %s", class)
2022-09-26 15:25:21 +00:00
continue
}
2023-05-29 01:08:52 +00:00
if strings.Contains(arg, ",") {
log.Debug().Msgf("found comma in arg %d, splitting argument by commas and remaking arg list", i)
args = append(args[:i], strings.Split(arg, ",")...)
log.Debug().Msgf("new args: %v", args)
continue
}
2022-09-26 15:25:21 +00:00
_, err := strconv.Atoi(arg)
if err != nil {
return err
}
ids = append(ids, arg)
}
2022-10-14 06:38:13 +00:00
resp, err := br.CreateGroup(huego.Group{Name: name, Lights: ids, Type: groupType, Class: class})
2022-09-26 15:25:21 +00:00
if err != nil {
return err
}
log.Info().Msgf("response: %v", resp)
case "schedule":
resp, err := br.CreateSchedule(&huego.Schedule{Name: args[1]})
if err != nil {
return err
}
log.Info().Msgf("response: %v", resp)
case "rule":
resp, err := br.CreateRule(&huego.Rule{Name: args[1]})
if err != nil {
return err
}
log.Info().Msgf("response: %v", resp)
case "sensor":
resp, err := br.CreateSensor(&huego.Sensor{Name: args[1]})
if err != nil {
return err
}
log.Info().Msgf("response: %v", resp)
default:
return errors.New("invalid target type")
}
return nil
}