-
Notifications
You must be signed in to change notification settings - Fork 28
feat: WithNotifySignal #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Adds a new WithNotifySignal
option to let users specify OS signals that will cancel the command’s context.
- Introduces
WithNotifySignal
tosettings
and sets upsignal.NotifyContext
when signals are provided. - Updates
Execute
infang.go
to wrap the root context with signal handling. - Extends the example in
example/main.go
to demonstrate cancellation on interrupt and kill.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
fang.go | Add signals field, WithNotifySignal option, and wrap context with signal.NotifyContext . |
example/main.go | Add a RunE demo command and call fang.Execute with WithNotifySignal(os.Interrupt, os.Kill) . |
Comments suppressed due to low confidence (3)
example/main.go:93
- Inside the
RunE
callback, use the local command instance (c.Println
) rather than the outercmd
variable to ensure output goes to the correct command’s I/O.
cmd.Println("Working...")
example/main.go:117
- Note that
os.Kill
(SIGKILL) cannot be caught or intercepted. Consider using a signal likesyscall.SIGTERM
for graceful shutdown.
fang.WithNotifySignal(os.Interrupt, os.Kill),
fang.go:95
- Add unit tests for
WithNotifySignal
to verify that providing signals correctly wraps the context withsignal.NotifyContext
and cancels on the expected signals.
func WithNotifySignal(signals ...os.Signal) Option {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow! What a nice feature!!
if len(opts.signals) > 0 { | ||
var cancel context.CancelFunc | ||
ctx, cancel = signal.NotifyContext(ctx, opts.signals...) | ||
defer cancel() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@caarlos0 I think this is in part not needed and in part not a responsability Fang should take.
os/signal.Notify
and os/signal.NotifyContext
are functions that not only notify you about signals in channels, but they also add a side-effect that the app won't be finished automatically anymore while receiving the signal. When the app calls os/signal.Notify
, it has the responsibility to finish the finish itself.
This means that os/signal.Notify
should basically be called always by the main app, and not by libraries.
This should also not be needed by CLI apps like Glow because the default Go behavior is what we want anyway, which is to simply finish the app. os/signal.Notify
is for when you want to run something before finishing the app. (Task uses it to give processes a few seconds to finish before we kill them, for example).
not sure about this yet.
based on this discussion: charmbracelet/glow#786 (comment)