@@ -12,27 +12,17 @@ import (
12
12
func main () {
13
13
var opt options
14
14
args , err := flags .Parse (& opt )
15
- if err != nil {
16
- _ , _ = fmt .Fprintln (os .Stderr , "cannot parse options:" , err )
17
- os .Exit (1 )
18
- }
15
+ exitWithTextIfError ("cannot parse options:" , err )
19
16
20
17
keys , err := findKeys (opt )
21
- if err != nil {
22
- _ , _ = fmt .Fprintln (os .Stderr , "cannot find IFTTT key(s):" , err )
23
- os .Exit (1 )
24
- }
18
+ exitWithTextIfError ("cannot find IFTTT key(s):" , err )
25
19
26
20
if len (keys ) == 0 {
27
- _ , _ = fmt .Fprintln (os .Stderr , "no IFTTT key(s) found" )
28
- os .Exit (1 )
21
+ exitWithText ("no IFTTT key(s) found" )
29
22
}
30
23
31
24
messages , err := findMessages (opt , args )
32
- if err != nil {
33
- _ , _ = fmt .Fprintln (os .Stderr , "cannot find messages:" , err )
34
- os .Exit (1 )
35
- }
25
+ exitWithTextIfError ("cannot find messages:" , err )
36
26
37
27
if len (messages ) == 0 {
38
28
messages = []string {" " }
@@ -48,22 +38,22 @@ func main() {
48
38
}(key )
49
39
}
50
40
51
- var err error
41
+ hasError := false
52
42
for range keys {
53
- err = <- errorChan
43
+ err := <- errorChan
44
+ if err != nil {
45
+ printError ("cannot send notification:" , err )
46
+ hasError = true
47
+ }
54
48
}
55
49
56
- if err != nil {
57
- _ , _ = fmt .Fprintln (os .Stderr , "cannot send notification:" , err )
50
+ if hasError {
58
51
os .Exit (1 )
59
52
}
60
53
} else {
61
54
for _ , key := range keys {
62
55
err := sendNotifications (key , opt .Event , opt .Title , messages , opt .Delay )
63
- if err != nil {
64
- _ , _ = fmt .Fprintln (os .Stderr , "cannot send notification:" , err )
65
- os .Exit (1 )
66
- }
56
+ exitWithTextIfError ("cannot send notification:" , err )
67
57
}
68
58
}
69
59
}
@@ -90,3 +80,18 @@ func sendNotifications(key Key, event, title string, messages []string, delay ti
90
80
91
81
return nil
92
82
}
83
+
84
+ func printError (args ... interface {}) {
85
+ _ , _ = fmt .Fprintln (os .Stderr , args ... )
86
+ }
87
+
88
+ func exitWithText (args ... interface {}) {
89
+ printError (args ... )
90
+ os .Exit (1 )
91
+ }
92
+
93
+ func exitWithTextIfError (text string , err error ) {
94
+ if err != nil {
95
+ exitWithText (text , err .Error ())
96
+ }
97
+ }
0 commit comments