-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgopushbullet.go
65 lines (58 loc) · 1.42 KB
/
gopushbullet.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
package gopushbullet
import (
"fmt"
"path/filepath"
"strings"
"github.com/spf13/viper"
pushbullet "github.com/xconstruct/go-pushbullet"
)
// PushBulletConfig - configuration for pushbullet
type PushBulletConfig struct {
Pushbulletchanneltag string `yaml:"pushbulletchanneltag"`
PushbulletAPIKey string `yaml:"pushbulletAPIKey"`
PushbulletEnable bool `yaml:"pushbulletEnable"`
}
// PBNotify struct for doing pushbullet notification
type PBNotify struct {
config *PushBulletConfig
}
func readConfig(path string) (*PushBulletConfig, error) {
v := viper.New()
v.AddConfigPath(filepath.Dir(path))
filename := filepath.Base(path)
v.SetConfigName(strings.TrimSuffix(filename, filepath.Ext(filename)))
err := v.ReadInConfig()
if err != nil {
return nil, err
}
var cf = &PushBulletConfig{}
err = v.UnmarshalKey("pbconfig", cf)
if err != nil {
return nil, err
}
return cf, nil
}
// New : Create notify
func New(path string) (*PBNotify, error) {
config, err := readConfig(path)
if err != nil {
return nil, err
}
return &PBNotify{
config: config,
}, nil
}
// Notify : push
func (notify *PBNotify) Notify(title string, msg interface{}) error {
config := notify.config
if !config.PushbulletEnable {
return nil
}
pb := pushbullet.New(config.PushbulletAPIKey)
sub, err := pb.Subscription(config.Pushbulletchanneltag)
if err != nil {
return err
}
pbmsg := fmt.Sprint(msg)
return sub.PushNote(title, pbmsg)
}