/
pushovercli.go
81 lines (75 loc) · 2.27 KB
/
pushovercli.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
"github.com/gregdel/pushover"
)
func mustAtoi(s string) (i int) {
i, _ = strconv.Atoi(s)
return
}
var (
argToken = flag.String("token", os.Getenv("PUSHOVER_TOKEN"), "Pushover application token")
argUser = flag.String("user", os.Getenv("PUSHOVER_USER"), "Pushover user token")
argTitle = flag.String("title", os.Getenv("PUSHOVER_TITLE"), "Notification title")
argURL = flag.String("url", os.Getenv("PUSHOVER_URL"), "Notification url")
argURLTitle = flag.String("url_title", os.Getenv("PUSHOVER_URL_TITLE"), "Notification url title")
argDevices = flag.String("devices", os.Getenv("PUSHOVER_DEVICES"), "Notification devices comma separated")
argPriority = flag.Int("priority", mustAtoi(os.Getenv("PUSHOVER_PRIORITY")), "Notification priority")
argSound = flag.String("sound", os.Getenv("PUSHOVER_SOUND"), "Notification sound")
argTimestamp = flag.String("timestamp", os.Getenv("PUSHOVER_TIMESTAMP"), "Notification timestamp")
argTimestampFormat = flag.String("timestamp_format", os.Getenv("PUSHOVER_TIMESTAMP_FORMAT"), "Notification timestamp format, e.g. Mon Jan 2 15:04:05 -0700 MST 2006")
)
func main() {
flag.Parse()
app := pushover.New(*argToken)
recipient := pushover.NewRecipient(*argUser)
body := strings.Join(flag.Args(), " ")
if body == "" {
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s", err)
os.Exit(2)
}
body = string(b)
}
msg := pushover.NewMessage(body)
if *argTitle != "" {
msg.Title = *argTitle
}
if *argURL != "" {
msg.URL = *argURL
}
if *argURLTitle != "" {
msg.URLTitle = *argURLTitle
}
if *argDevices != "" {
msg.DeviceName = *argDevices
}
msg.Priority = *argPriority
if *argSound != "" {
msg.Sound = *argSound
}
if *argTimestamp != "" {
ts, err := time.Parse(*argTimestampFormat, *argTimestamp)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing timestamp: %s\n", err)
os.Exit(1)
}
msg.Timestamp = ts.Unix()
}
resp, err := app.SendMessage(msg, recipient)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s", err)
os.Exit(1)
}
if len(resp.Errors) > 0 {
fmt.Fprintf(os.Stderr, "Error: %s", err)
os.Exit(2)
}
}