-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alter pubic interface for signal context.
Change ContextWithCancelSignals and ProgramContext to have an API that looks more familiar to the standard library context.WithCancel and context.WithDeadline. This also allows SignalContext to be made private. Move ExitHint to be a function, not a SignalContext method. Also introduce an ExitHinter interface that can be implemented by error types to suggest appropriate error codes when there is no signal received. Update example code.
- Loading branch information
Showing
3 changed files
with
72 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package op | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"syscall" | ||
) | ||
|
||
// ExitHinter is an interface that if implemented by an error type, will be | ||
// used within the ExitHint function. | ||
type ExitHinter interface { | ||
ExitHint() int | ||
} | ||
|
||
// ExitHint returns an exit code hint according to the passed in signal and | ||
// error. On Unix systems, 128 + int(signal) is returned when err is not nil. | ||
func ExitHint(signal os.Signal, err error) int { | ||
s, unixSignal := signal.(syscall.Signal) | ||
var eh ExitHinter | ||
switch { | ||
case err == nil: | ||
return 0 | ||
case unixSignal: | ||
return 128 + int(s) | ||
case errors.As(err, &eh): | ||
return eh.ExitHint() | ||
default: | ||
return 1 | ||
} | ||
} |