Feat: get command

This commit is contained in:
kayos@tcp.direct 2023-01-23 07:39:43 -08:00
parent 784952c148
commit bf9f3e19c4
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
2 changed files with 85 additions and 2 deletions

View File

@ -82,6 +82,7 @@ func init() {
Commands["dump"] = newZiggsCommand(cmdDump, "dump target object JSON to a file", 1)
Commands["load"] = newZiggsCommand(cmdLoad, "load JSON from a file into the bridge", 1)
Commands["set"] = newZiggsCommand(cmdSet, "update object properties in bridge", 3)
Commands["get"] = newZiggsCommand(cmdGet, "get object properties from bridge", 2)
Commands["upgrade"] = newZiggsCommand(cmdFirmwareUpdate, "inform bridge to check for updates", 0,
"fwup", "upgrade", "fwupdate")
Commands["info"] = newZiggsCommand(cmdInfo, "show information about a bridge", 0, "uname")
@ -136,8 +137,10 @@ func initCompletion() {
}
for _, sug := range suggestions[1] {
sug.requires = map[int]map[string]bool{1: {
"delete": true, "del": true, "set": true, "s": true, "rename": true, "mv": true, "dump": true, "load": true},
}
"delete": true, "del": true, "set": true, "s": true,
"rename": true, "mv": true, "dump": true, "load": true,
"get": true,
}}
sug.root = false
}
delCompletion := []*completion{

80
internal/cli/get.go Normal file
View File

@ -0,0 +1,80 @@
package cli
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/amimof/huego"
"git.tcp.direct/kayos/ziggs/internal/ziggy"
)
// cmdGet is used to get the state(s) of lights and groups. it outputs the data in tab-delimited JSON.
func cmdGet(bridge *ziggy.Bridge, args []string) error {
if len(args) < 2 {
return errors.New("not enough arguments")
}
var (
groupMap map[string]*huego.Group
lightMap map[string]*ziggy.HueLight
currentState *huego.State
argHead = -1
)
for range args {
argHead++
if len(args) <= argHead {
break
}
log.Trace().Int("argHead", argHead).Msg(args[argHead])
switch args[argHead] {
case "group", "g":
groupMap = ziggy.GetGroupMap()
if len(args) <= argHead-1 {
return errors.New("no group specified")
}
argHead++
g, ok := groupMap[strings.TrimSpace(args[argHead])]
if !ok {
return fmt.Errorf("group %s not found (argHead: %d)", args[argHead], argHead)
}
log.Trace().Str("group", g.Name).Msgf("found group %s via args[%d]",
args[argHead], argHead,
)
currentState = g.State
case "light", "l":
lightMap = ziggy.GetLightMap()
if len(args) <= argHead-1 {
return errors.New("no light specified")
}
argHead++
l, ok := lightMap[strings.TrimSpace(args[argHead])]
if !ok {
return fmt.Errorf("light %s not found (argHead: %d)", args[argHead], argHead)
}
if extraDebug {
log.Trace().Str("group", l.Name).Msgf("found light %s via args[%d]",
args[argHead], argHead)
}
currentState = l.State
}
}
if currentState == nil {
return errors.New("no state found")
}
data, err := json.MarshalIndent(currentState, "", "\t")
if err != nil {
return err
}
data = append(data, '\n')
_, err = io.Copy(os.Stdout, bytes.NewReader(data))
return err
}