forked from bpicode/fritzctl
/
list_alerts.go
49 lines (43 loc) · 1.26 KB
/
list_alerts.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
package cmd
import (
"os"
"github.com/coolduke/fritzctl/cmd/printer"
"github.com/coolduke/fritzctl/fritz"
"github.com/coolduke/fritzctl/internal/console"
"github.com/coolduke/fritzctl/internal/stringutils"
"github.com/coolduke/fritzctl/logger"
"github.com/spf13/cobra"
)
var listAlertsCmd = &cobra.Command{
Use: "alerts",
Short: "List recent alert sensor reports",
Long: "List the most recently reported state of all devices with an alert sensor.",
Example: "fritzctl list alerts",
RunE: listAlerts,
}
func init() {
listAlertsCmd.Flags().StringP("output", "o", "", "specify output format")
listCmd.AddCommand(listAlertsCmd)
}
func listAlerts(cmd *cobra.Command, _ []string) error {
devs := mustList()
data := selectFmt(cmd, devs.AlertSensors(), alertSensorsTable)
logger.Success("Device data:")
printer.Print(data, os.Stdout)
return nil
}
func alertSensorsTable(devs []fritz.Device) interface{} {
table := console.NewTable(console.Headers(
"NAME",
"ALERT",
))
symbols := stringutils.MapWithDefault(map[string]string{
"0": console.Green("\u2714"),
"1": console.Yellow("\u26a0"),
}, console.Yellow("?"))
for _, dev := range devs {
columns := []string{dev.Name, symbols(dev.AlertSensor.State)}
table.Append(columns)
}
return table
}