-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (64 loc) · 1.54 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
package main
import (
"errors"
"fmt"
"os"
"github.com/lorenzobenvenuti/ifttt"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
app = kingpin.New("iftttclient", "A command-line IFTTT client application")
triggerCmd = app.Command("trigger", "Triggers an event")
triggerApiKey = triggerCmd.Flag("api-key", "IFTTT API key").Short('k').String()
triggerEvent = triggerCmd.Arg("event", "Event to trigger").Required().String()
triggerValues = triggerCmd.Arg("values", "Values to pass").Strings()
storeCmd = app.Command("store", "Stores the IFTTT API key for later use")
storeApiKey = storeCmd.Arg("api-key", "IFTTT API key").Required().String()
)
func exit(err interface{}) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
func getApiKeyFromStore() (string, error) {
return NewStore("").Retrieve()
}
func getApiKey() (string, error) {
key := *triggerApiKey
if key == "" {
key = os.Getenv("IFTTT_API_KEY")
if key == "" {
key, _ = getApiKeyFromStore()
}
}
if key == "" {
return "", errors.New("Please specify an API key")
}
return key, nil
}
func trigger() error {
key, err := getApiKey()
if err != nil {
exit(err)
}
iftttClient := ifttt.NewIftttClient(key)
return iftttClient.Trigger(*triggerEvent, *triggerValues)
}
func store() {
NewStore("").Store(*storeApiKey)
}
func main() {
defer func() {
if err := recover(); err != nil {
exit(err)
}
}()
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case triggerCmd.FullCommand():
err := trigger()
if err != nil {
panic(err)
}
case storeCmd.FullCommand():
store()
}
}