Skip to content

Commit 50c2fd5

Browse files
authored
Merge pull request #93 from atc0005/update-exit-state-handling-to-reflect-dep-changes
Update exit state handling to reflect dep changes
2 parents fd58931 + fb1582f commit 50c2fd5

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

cmd/check_imap_mailbox/main.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ func main() {
3737
ExitStatusCode: nagios.StateOKExitCode,
3838
}
3939

40+
// defer this from the start so it is the last deferred function to run
41+
defer nagiosExitState.ReturnCheckResults()
42+
4043
// Setup configuration by parsing user-provided flags
4144
cfg := config.Config{}
4245

@@ -50,7 +53,7 @@ func main() {
5053
)
5154
nagiosExitState.LastError = err
5255
nagiosExitState.ExitStatusCode = nagios.StateCRITICALExitCode
53-
nagiosExitState.ReturnCheckResults()
56+
return
5457
}
5558

5659
if cfg.EmitBranding {
@@ -85,7 +88,7 @@ func main() {
8588
nagios.StateCRITICALLabel,
8689
)
8790
nagiosExitState.ExitStatusCode = nagios.StateCRITICALExitCode
88-
nagiosExitState.ReturnCheckResults()
91+
return
8992
}
9093

9194
server := fmt.Sprintf("%s:%d", cfg.Server, cfg.Port)
@@ -101,7 +104,7 @@ func main() {
101104
server,
102105
)
103106
nagiosExitState.ExitStatusCode = nagios.StateCRITICALExitCode
104-
nagiosExitState.ReturnCheckResults()
107+
return
105108
}
106109
log.Debug().Msg("Connected")
107110

@@ -114,7 +117,7 @@ func main() {
114117
nagios.StateCRITICALLabel,
115118
)
116119
nagiosExitState.ExitStatusCode = nagios.StateCRITICALExitCode
117-
nagiosExitState.ReturnCheckResults()
120+
return
118121
}
119122
log.Debug().Msg("Logged in")
120123

@@ -137,14 +140,14 @@ func main() {
137140
nagios.StateWARNINGLabel,
138141
)
139142
nagiosExitState.ExitStatusCode = nagios.StateWARNINGExitCode
140-
nagiosExitState.ReturnCheckResults()
143+
return
141144
}
142145
}(cfg.Username)
143146

144147
// Generate background job to list mailboxes, send down channel until done
145148
mailboxes := make(chan *imap.MailboxInfo, 10)
146149
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
148151
go func() {
149152
log.Debug().Msg("Running c.List() to fetch a list of available mailboxes")
150153
done <- c.List("", "*", mailboxes)
@@ -156,11 +159,6 @@ func main() {
156159
mailboxesList = append(mailboxesList, m.Name)
157160
}
158161

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?
164162
if err := <-done; err != nil {
165163
log.Error().Err(err).Msg("Error occurred listing mailboxes")
166164
nagiosExitState.LastError = err
@@ -169,7 +167,7 @@ func main() {
169167
nagios.StateCRITICALLabel,
170168
)
171169
nagiosExitState.ExitStatusCode = nagios.StateCRITICALExitCode
172-
nagiosExitState.ReturnCheckResults()
170+
return
173171
}
174172

175173
log.Debug().Msg("no errors encountered listing mailboxes")
@@ -220,7 +218,7 @@ func main() {
220218
nagios.StateCRITICALLabel,
221219
)
222220
nagiosExitState.ExitStatusCode = nagios.StateCRITICALExitCode
223-
nagiosExitState.ReturnCheckResults()
221+
return
224222
}
225223

226224
}
@@ -246,7 +244,7 @@ func main() {
246244
folder,
247245
)
248246
nagiosExitState.ExitStatusCode = nagios.StateCRITICALExitCode
249-
nagiosExitState.ReturnCheckResults()
247+
return
250248
}
251249

252250
log.Debug().Str("mailbox", folder).Msgf("Mailbox flags: %v", mailbox.Flags)
@@ -275,7 +273,7 @@ func main() {
275273
results.MessagesFoundSummary(),
276274
)
277275
nagiosExitState.ExitStatusCode = nagios.StateWARNINGExitCode
278-
nagiosExitState.ReturnCheckResults()
276+
return
279277
}
280278

281279
// Give the all clear: no mail was found
@@ -288,5 +286,6 @@ func main() {
288286
cfg.Folders.String(),
289287
)
290288
nagiosExitState.ExitStatusCode = nagios.StateOKExitCode
289+
// implied return here :)
291290

292291
}

0 commit comments

Comments
 (0)