/
main.go
162 lines (144 loc) · 3.17 KB
/
main.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
package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/dhulihan/adafruit-io/aio"
"os"
)
func init() {
log.SetLevel(log.InfoLevel)
}
func main() {
app := cli.NewApp()
app.Name = "adafruit-io"
app.Version = "1.0.0"
app.Usage = "Send data to your adafruit.io dashboard"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "format, f",
Value: "text",
Usage: "Desired output format. Options: json, text (default)",
}, cli.StringFlag{
Name: "key, k",
Usage: "Your adafruit.io secret key. $AIO_KEY tried first",
EnvVar: "AIO_KEY",
},
cli.BoolFlag{
Name: "debug, d",
Usage: "Enable to see debug messages",
},
}
app.Commands = []cli.Command{
{
Name: "feeds",
Aliases: []string{"f"},
Usage: "Get a list of all feeds",
Action: FeedsAction,
},
{
Name: "get",
Aliases: []string{"g"},
Usage: "Get feeds last value",
Description: "get <FEED ID|FEED NAME|FEED KEY>",
Action: GetAction,
},
{
Name: "send",
Aliases: []string{"s"},
Usage: "Update a feed's last_value",
Description: "send <FEED ID|FEED NAME|FEED KEY> <VALUE>",
Action: SendAction,
},
{
Name: "key",
Aliases: []string{"k"},
Usage: "print AIO key",
Action: KeyAction,
},
}
Run(app)
}
func Run(app *cli.App) {
app.Before = func(c *cli.Context) error {
if c.GlobalString("key") == "" {
log.Fatal("No aio key provided. Use --key KEY_HERE or export AIO_KEY=KEY_HERE")
}
if c.GlobalBool("debug") {
log.SetLevel(log.DebugLevel)
log.Debug("Debug Mode ON")
log.Debug("AIO_KEY: ", c.GlobalString("key"))
}
return nil
}
app.Run(os.Args)
}
func FeedsAction(c *cli.Context) {
a := aio.NewContext(c.GlobalString("key"))
feeds, err := aio.Feeds(&a)
if err != nil {
log.Fatal(err)
}
if len(feeds) > 0 {
for _, feed := range feeds {
fmt.Println(feed.Name)
}
} else {
fmt.Println("No feeds found.")
}
}
func KeyAction(c *cli.Context) {
fmt.Println(c.GlobalString("key"))
}
func InfoAction(c *cli.Context) {
log.Debug("Args: ", c.Args())
if len(c.Args()) == 0 {
log.Fatal("feed id missing")
}
id := c.Args().First()
for k, v := range FetchFeed(c, id) {
fmt.Printf("%s: %s\n", k, v)
}
}
func FetchFeed(c *cli.Context, id string) map[string]interface{} {
return nil
}
func GetAction(c *cli.Context) {
log.Debug("Args: ", c.Args())
if len(c.Args()) == 0 {
log.Fatal("feed id missing")
}
a := aio.NewContext(c.GlobalString("key"))
id := c.Args().First()
feed, err := aio.Find(id, &a)
if err != nil {
log.Fatal(err)
}
last_value := feed.Last_Value
if last_value != "" {
fmt.Println(last_value)
} else {
log.Fatal("last_value not set")
}
}
func SendAction(c *cli.Context) {
log.Debug("Args: ", c.Args())
if len(c.Args()) == 0 {
log.Fatal("feed id missing")
}
if len(c.Args()) == 1 {
log.Fatal("value is missing")
}
a := aio.NewContext(c.GlobalString("key"))
id := c.Args().First()
val := c.Args()[len(c.Args())-1]
err := aio.Send(id, val, &a)
if err != nil {
log.Fatal(err)
} else {
fmt.Println("OK", val)
}
}
func FeedInfo(c *cli.Context, id string) map[string]interface{} {
return nil
}