-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrowl.go
76 lines (65 loc) · 1.42 KB
/
growl.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 (
"fmt"
"io/ioutil"
"os"
"github.com/andrew-d/go-termutil"
api "github.com/mattn/go-gntp"
)
type growlCmd struct {
_ struct{} `help:"notify by Growl(GNTP)"`
Send growlSendCmd `help:"send a notification"`
}
type growlSendCmd struct {
Server string `default:"localhost" help:"Growl server"`
Port int `default:"23053" help:"Growl server port"`
Title string `default:"xn" help:"title"`
Event string `default:"default" help:"event"`
Message string `help:"message"`
}
func (c growlSendCmd) Run(global globalCmd, args []string) error {
//
// prepare
//
for _, v := range args {
if len(c.Message) > 0 {
c.Message += "\n"
}
c.Message += v
}
if !termutil.Isatty(os.Stdin.Fd()) {
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
bytes = []byte{}
}
if len(c.Message) == 0 {
c.Message = string(bytes)
} else if len(bytes) != 0 {
c.Message += "\n" + string(bytes)
}
}
if len(c.Message) == 0 {
return nil
}
//
// send
//
client := api.NewClient()
client.AppName = "xn"
client.Server = fmt.Sprintf("%s:%d", c.Server, c.Port)
n := []api.Notification{{c.Event, c.Event, true}}
client.Register(n)
client.Notify(&api.Message{
Event: c.Event,
Title: c.Title,
Text: c.Message,
//Icon: icon,
//Callback: url,
DisplayName: c.Event,
//Sticky: *sticky,
})
return nil
}
func init() {
appendCommand(&growlCmd{}, "growl, gr", "")
}