Skip to content

Conversation

caarlos0
Copy link
Member

not sure about this yet.

based on this discussion: charmbracelet/glow#786 (comment)

@caarlos0 caarlos0 requested a review from Copilot June 26, 2025 22:57
@caarlos0 caarlos0 self-assigned this Jun 26, 2025
@caarlos0 caarlos0 added the enhancement New feature or request label Jun 26, 2025
Copy link
Contributor

@Copilot Copilot AI left a 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 to settings and sets up signal.NotifyContext when signals are provided.
  • Updates Execute in fang.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 outer cmd 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 like syscall.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 with signal.NotifyContext and cancels on the expected signals.
func WithNotifySignal(signals ...os.Signal) Option {

@caarlos0 caarlos0 marked this pull request as ready for review June 27, 2025 14:23
Copy link
Member

@meowgorithm meowgorithm left a 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!!

@caarlos0 caarlos0 merged commit 965d86e into main Jun 27, 2025
18 checks passed
@caarlos0 caarlos0 deleted the signals branch June 27, 2025 15:00
Comment on lines +149 to +153
if len(opts.signals) > 0 {
var cancel context.CancelFunc
ctx, cancel = signal.NotifyContext(ctx, opts.signals...)
defer cancel()
}
Copy link
Member

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants