forked from deepu105/keylight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
52 lines (45 loc) · 903 Bytes
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/urfave/cli/v2"
)
var listCommand = &cli.Command{
Name: "list",
Aliases: []string{"l"},
Usage: "Discover and list available lights",
Flags: []cli.Flag{
&timeoutFlag,
},
Action: listAction,
}
func listAction(c *cli.Context) error {
timeout := time.Duration(c.Int("timeout")) * time.Second
fmt.Println("Command: List - lights: all")
devicesCh, err := discoverLights()
if err != nil {
return err
}
tbl := createTable()
count := 0
for {
select {
case device := <-devicesCh:
if device == nil {
return nil
}
group, err := device.FetchLightGroup(context.Background())
if err != nil {
log.Println("failed to retrieve light group: ", err.Error())
return err
}
addTableRow(tbl, device, group)
count++
case <-time.After(timeout):
drawTable(tbl, count)
return nil
}
}
}