@@ -37,6 +37,9 @@ func main() {
37
37
ExitStatusCode : nagios .StateOKExitCode ,
38
38
}
39
39
40
+ // defer this from the start so it is the last deferred function to run
41
+ defer nagiosExitState .ReturnCheckResults ()
42
+
40
43
// Setup configuration by parsing user-provided flags
41
44
cfg := config.Config {}
42
45
@@ -50,7 +53,7 @@ func main() {
50
53
)
51
54
nagiosExitState .LastError = err
52
55
nagiosExitState .ExitStatusCode = nagios .StateCRITICALExitCode
53
- nagiosExitState . ReturnCheckResults ()
56
+ return
54
57
}
55
58
56
59
if cfg .EmitBranding {
@@ -85,7 +88,7 @@ func main() {
85
88
nagios .StateCRITICALLabel ,
86
89
)
87
90
nagiosExitState .ExitStatusCode = nagios .StateCRITICALExitCode
88
- nagiosExitState . ReturnCheckResults ()
91
+ return
89
92
}
90
93
91
94
server := fmt .Sprintf ("%s:%d" , cfg .Server , cfg .Port )
@@ -101,7 +104,7 @@ func main() {
101
104
server ,
102
105
)
103
106
nagiosExitState .ExitStatusCode = nagios .StateCRITICALExitCode
104
- nagiosExitState . ReturnCheckResults ()
107
+ return
105
108
}
106
109
log .Debug ().Msg ("Connected" )
107
110
@@ -114,7 +117,7 @@ func main() {
114
117
nagios .StateCRITICALLabel ,
115
118
)
116
119
nagiosExitState .ExitStatusCode = nagios .StateCRITICALExitCode
117
- nagiosExitState . ReturnCheckResults ()
120
+ return
118
121
}
119
122
log .Debug ().Msg ("Logged in" )
120
123
@@ -137,14 +140,14 @@ func main() {
137
140
nagios .StateWARNINGLabel ,
138
141
)
139
142
nagiosExitState .ExitStatusCode = nagios .StateWARNINGExitCode
140
- nagiosExitState . ReturnCheckResults ()
143
+ return
141
144
}
142
145
}(cfg .Username )
143
146
144
147
// Generate background job to list mailboxes, send down channel until done
145
148
mailboxes := make (chan * imap.MailboxInfo , 10 )
146
149
done := make (chan error , 1 )
147
- // TODO: Aside from app exit, what shuts down this goroutine?
150
+ // NOTE: This goroutine shuts down once c.List() finishes its work
148
151
go func () {
149
152
log .Debug ().Msg ("Running c.List() to fetch a list of available mailboxes" )
150
153
done <- c .List ("" , "*" , mailboxes )
@@ -156,11 +159,6 @@ func main() {
156
159
mailboxesList = append (mailboxesList , m .Name )
157
160
}
158
161
159
- // At this point we are finished with the done channel? At what point
160
- // should we *move on* after wrapping up our use of the channel? The
161
- // official README "client" example shows checking the channel results
162
- // *after* ranging over it, so presumably it doesn't need to be checked
163
- // upfront? Why is this?
164
162
if err := <- done ; err != nil {
165
163
log .Error ().Err (err ).Msg ("Error occurred listing mailboxes" )
166
164
nagiosExitState .LastError = err
@@ -169,7 +167,7 @@ func main() {
169
167
nagios .StateCRITICALLabel ,
170
168
)
171
169
nagiosExitState .ExitStatusCode = nagios .StateCRITICALExitCode
172
- nagiosExitState . ReturnCheckResults ()
170
+ return
173
171
}
174
172
175
173
log .Debug ().Msg ("no errors encountered listing mailboxes" )
@@ -220,7 +218,7 @@ func main() {
220
218
nagios .StateCRITICALLabel ,
221
219
)
222
220
nagiosExitState .ExitStatusCode = nagios .StateCRITICALExitCode
223
- nagiosExitState . ReturnCheckResults ()
221
+ return
224
222
}
225
223
226
224
}
@@ -246,7 +244,7 @@ func main() {
246
244
folder ,
247
245
)
248
246
nagiosExitState .ExitStatusCode = nagios .StateCRITICALExitCode
249
- nagiosExitState . ReturnCheckResults ()
247
+ return
250
248
}
251
249
252
250
log .Debug ().Str ("mailbox" , folder ).Msgf ("Mailbox flags: %v" , mailbox .Flags )
@@ -275,7 +273,7 @@ func main() {
275
273
results .MessagesFoundSummary (),
276
274
)
277
275
nagiosExitState .ExitStatusCode = nagios .StateWARNINGExitCode
278
- nagiosExitState . ReturnCheckResults ()
276
+ return
279
277
}
280
278
281
279
// Give the all clear: no mail was found
@@ -288,5 +286,6 @@ func main() {
288
286
cfg .Folders .String (),
289
287
)
290
288
nagiosExitState .ExitStatusCode = nagios .StateOKExitCode
289
+ // implied return here :)
291
290
292
291
}
0 commit comments