-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprowlsend.go
205 lines (182 loc) · 4.83 KB
/
prowlsend.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"flag"
"fmt"
"os"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/BurntSushi/toml"
"github.com/rem7/goprowl"
)
const VERSION = "1.0.0"
var Options struct {
Priority int
Event string
Url string
Application string
HostPrefix bool
ConfigFile string
}
var printVersion bool
const CONFIG_PATH = "prowl/prowl.toml"
type Config struct {
ApiKey string
}
func ReadConfig(path string) (*Config, error) {
var config Config
if _, err := toml.DecodeFile(path, &config); err != nil {
return nil, err
}
return &config, nil
}
func ConfigFileLocations(configpath string) []string {
locations := []string{}
if runtime.GOOS == "windows" {
configdirs := []string{}
appdata := os.Getenv("APPDATA")
if len(appdata) > 0 {
configdirs = append(configdirs, appdata)
}
localappdata := os.Getenv("LOCALAPPDATA")
if len(localappdata) > 0 {
configdirs = append(configdirs, localappdata)
}
for _, configdir := range configdirs {
locations = append(locations, filepath.Join(configdir, configpath))
}
} else {
for _, path := range strings.Split(os.Getenv("XDG_CONFIG_DIRS"), ":") {
if len(path) > 0 {
locations = append(locations, filepath.Join(path, configpath))
}
}
if len(locations) == 0 {
user, _ := user.Current()
if user != nil && user.HomeDir != "" {
locations = append(locations,
filepath.Join(user.HomeDir, ".config", configpath))
}
}
}
return locations
}
func FindConfigFile(configpath string) (string, error) {
var locations []string
if len(Options.ConfigFile) > 0 {
locations = []string{Options.ConfigFile}
} else {
locations = ConfigFileLocations(configpath)
}
for _, path := range locations {
if fi, err := os.Stat(path); err == nil {
if fi.Mode().IsRegular() {
perm := fi.Mode().Perm()
if (perm & 0027) != 0 {
fmt.Fprintf(os.Stderr,
"** WARNING: file permission for \"%s\" are too large\n",
path)
}
return path, nil
}
}
}
var err error
if len(locations) > 0 {
err = fmt.Errorf("%#v not found", filepath.Join(locations[0], configpath))
} else {
err = fmt.Errorf("Configuration path must be given with `-c` option")
}
return "", err
}
func main() {
flag.Parse()
if printVersion {
fmt.Println(VERSION)
os.Exit(0)
}
configpath, err := FindConfigFile(CONFIG_PATH)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
config, err := ReadConfig(configpath)
if config == nil {
fmt.Fprintf(os.Stderr, "Error:%s: %v\n", configpath, err)
os.Exit(1)
}
if len(config.ApiKey) == 0 {
fmt.Fprintf(os.Stderr, "Error: API key not found in configuration file\n")
os.Exit(1)
}
var p goprowl.Goprowl
if err := p.RegisterKey(config.ApiKey); err != nil {
fmt.Fprintf(os.Stderr, "Error registering key: %v\n", err)
os.Exit(1)
}
app := Options.Application
if Options.HostPrefix {
if hostname, ok := os.Hostname(); ok == nil {
app = app + " on " + hostname
}
}
n := &goprowl.Notification{
Application: app,
Description: "",
Event: Options.Event,
Priority: strconv.Itoa(Options.Priority),
Url: Options.Url,
}
switch len(flag.Args()) {
case 0:
fmt.Fprintln(os.Stderr, "Error: need at least a message as argument")
os.Exit(1)
default:
n.Description = strings.Join(flag.Args(), "\n")
}
err = p.Push(n)
if err != nil {
fmt.Fprintln(os.Stderr, "Error: failed to send Prowl notification")
fmt.Fprintln(os.Stderr, "# %r", err)
os.Exit(1)
}
}
func usage() {
locations := ConfigFileLocations(CONFIG_PATH)
fmt.Fprintf(os.Stderr, "Usage: %s [options] \"message\" [...]\n",
filepath.Base(os.Args[0]))
fmt.Fprintf(os.Stderr, "version %s\n", VERSION)
fmt.Fprintf(os.Stderr, "%s", `
Messages are concatenated with a carriage return character between them.
`)
if len(locations) > 1 {
fmt.Fprintf(os.Stderr, "%s%s\n", `
Default configuration files can be found at:
- `, strings.Join(locations, "\n - "))
} else if len(locations) == 1 {
fmt.Fprintf(os.Stderr, "%s%s\n", `
Default configuration file can be found at:
- `, locations[0])
} else {
fmt.Fprintf(os.Stderr, "%s\n", `
Cannot find a default configuration file location.
You must provide a configuration file with '-c' option.`)
}
fmt.Fprintf(os.Stderr, `
Options:
`)
flag.PrintDefaults()
}
func init() {
flag.IntVar(&Options.Priority, "p", 1, "notification `priority`")
flag.StringVar(&Options.Event, "e", "", "notification optional `event`")
flag.StringVar(&Options.Url, "u", "", "notification optional `URL`")
flag.StringVar(&Options.Application, "a", "Prowlsend", "application `name`")
flag.BoolVar(&Options.HostPrefix, "o", true,
"Use \"-o=false\" to not append hostname in application name")
flag.StringVar(&Options.ConfigFile, "c", "", "`path` to specific configuration file")
flag.BoolVar(&printVersion, "v", false, "print program version")
flag.Usage = usage
}