/
hdp.go
163 lines (153 loc) · 3.93 KB
/
hdp.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"os"
"os/user"
"net"
"strconv"
"strings"
"github.com/urfave/cli"
"github.com/urfave/cli/altsrc"
"github.com/olekukonko/tablewriter"
"github.com/vincer/libhdplatinum"
)
func validate(ip string, port int, c *cli.Context) *cli.ExitError {
if strings.TrimSpace(ip) == "" {
return cli.NewExitError("IP address is required. `hdp -h` for usage info.", 1)
}
if net.ParseIP(ip) == nil {
return cli.NewExitError("Invalid IP address", 1)
}
if port < 0 || port > 65535 {
return cli.NewExitError("Invalid port", 2)
}
return nil
}
func getUserHome() string {
usr, _ := user.Current()
return usr.HomeDir
}
func main() {
app := cli.NewApp()
app.Name = "hdp"
app.Usage = "Hunter Douglas Platinum CLI"
app.Version = "0.0.1"
app.HideHelp = true
app.EnableBashCompletion = true
var ip string
var port int
home := getUserHome()
flags := []cli.Flag{
altsrc.NewStringFlag(cli.StringFlag{
Name: "ip",
Usage: "ip address of the Hunter Douglas Platinum Gateway. Required.",
Destination: &ip,
}),
cli.IntFlag{
Name: "port",
Value: 522,
Usage: "port of the Hunter Douglas Platinum Gateway.",
Destination: &port,
},
cli.StringFlag{
Name: "config, c",
Value: home + "/.hdp.yml",
Usage: "YAML config file. Useful for saving IP address of gateway.",
},
cli.BoolFlag{
Name: "help, h",
Usage: "Show usage help.",
},
}
app.Before = func(c *cli.Context) error {
filePath := c.String("config")
if _, err := os.Stat(filePath); err == nil {
return altsrc.InitInputSourceWithContext(flags, altsrc.NewYamlSourceFromFlagFunc("config"))(c)
}
return nil
}
app.Flags = flags
app.Commands = []cli.Command{
{
Name: "shades",
Usage: "Shade commands",
Subcommands: []cli.Command{
{
Name: "list",
Aliases: []string{"ls"},
Usage: "List shades",
Action: func(c *cli.Context) error {
exitError := validate(ip, port, c)
if (exitError != nil) {
return exitError
}
shades := libhdplatinum.GetShades(ip, port)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Height", "id"})
for _, shade := range shades {
table.Append([]string{shade.Name(), strconv.Itoa(shade.Height()), shade.Id()})
}
table.SetBorder(false)
table.SetAutoFormatHeaders(false)
table.SetCenterSeparator("-")
table.Render()
return nil
},
},
{
Name: "set",
Usage: "set <shade id or name> <height 0-255>",
Action: func(c *cli.Context) {
shades := libhdplatinum.GetShades(ip, port)
shadeIdOrName := c.Args().First()
for _, shade := range shades {
if shadeIdOrName == shade.Id() || shadeIdOrName == shade.Name() {
height, _ := strconv.Atoi(c.Args().Get(1))
shade.SetHeight(height)
}
}
},
},
},
},
{
Name: "rooms",
Usage: "Room commands",
Subcommands: []cli.Command{
{
Name: "list",
Aliases: []string{"ls"},
Usage: "List rooms",
Action: func(c *cli.Context) {
rooms := libhdplatinum.GetRooms(ip, port)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Number of Shades", "id"})
for _, room := range rooms {
table.Append([]string{room.Name(), strconv.Itoa(len(room.Shades())), room.Id()})
}
table.SetBorder(false)
table.SetAutoFormatHeaders(false)
table.SetCenterSeparator("-")
table.Render()
},
},
{
Name: "set",
Usage: "set <room id or name> <height 0-255>",
Action: func(c *cli.Context) {
rooms := libhdplatinum.GetRooms(ip, port)
roomIdOrName := c.Args().First()
for _, room := range rooms {
if roomIdOrName == room.Id() || roomIdOrName == room.Name() {
height, _ := strconv.Atoi(c.Args().Get(1))
for _, shade := range room.Shades() {
shade.SetHeight(height)
}
}
}
},
},
},
},
}
app.Run(os.Args)
}