blob: b889d3b4f94ebc4cdb0a08d6fe6725d3b9fb26da [file] [log] [blame]
package command
import (
"context"
"os"
"os/signal"
)
// CancelOnSignals returns a Context that emits a Done event when any of the input signals
// are recieved, assuming those signals can be handled by the current process.
func CancelOnSignals(ctx context.Context, sigs ...os.Signal) context.Context {
ctx, cancel := context.WithCancel(ctx)
signals := make(chan os.Signal)
signal.Notify(signals, sigs...)
go func() {
select {
case s := <-signals:
if s != nil {
cancel()
close(signals)
}
}
}()
return ctx
}