Remove os.Exit() from library code (gt-fm75)
Refactor to return errors instead of calling os.Exit() directly: - Add SilentExitError type for commands that signal status via exit code - Update mail.go runMailPeek() and runMailCheck() to return errors - Change Execute() to return int exit code instead of calling os.Exit() - Move os.Exit() call to main() where it belongs This improves testability, enables graceful shutdown, and follows Go conventions for library code. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
31
internal/cmd/errors.go
Normal file
31
internal/cmd/errors.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package cmd
|
||||
|
||||
import "fmt"
|
||||
|
||||
// SilentExitError signals that the command should exit with a specific code
|
||||
// without printing an error message. This is used for scripting purposes
|
||||
// where exit codes convey status (e.g., "no mail" = exit 1).
|
||||
type SilentExitError struct {
|
||||
Code int
|
||||
}
|
||||
|
||||
func (e *SilentExitError) Error() string {
|
||||
return fmt.Sprintf("exit %d", e.Code)
|
||||
}
|
||||
|
||||
// NewSilentExit creates a SilentExitError with the given exit code.
|
||||
func NewSilentExit(code int) *SilentExitError {
|
||||
return &SilentExitError{Code: code}
|
||||
}
|
||||
|
||||
// IsSilentExit checks if an error is a SilentExitError and returns its code.
|
||||
// Returns 0 and false if err is nil or not a SilentExitError.
|
||||
func IsSilentExit(err error) (int, bool) {
|
||||
if err == nil {
|
||||
return 0, false
|
||||
}
|
||||
if se, ok := err.(*SilentExitError); ok {
|
||||
return se.Code, true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
Reference in New Issue
Block a user