@@ -1,9 +1,11 @@
11package cmd
22
33import (
4+ "context"
45 "fmt"
56 "log/slog"
67 "os"
8+ "os/signal"
79 "strings"
810
911 "github.com/spf13/cobra"
@@ -30,6 +32,10 @@ crush run --quiet "Generate a README for this project"
3032 RunE: func(cmd *cobra.Command, args []string) error {
3133 quiet, _ := cmd.Flags().GetBool("quiet")
3234
35+ // Cancel on SIGINT or SIGTERM.
36+ ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
37+ defer cancel()
38+
3339 app, err := setupApp(cmd)
3440 if err != nil {
3541 return err
@@ -57,8 +63,7 @@ crush run --quiet "Generate a README for this project"
5763 // crush run "Do something fancy" > output.txt
5864 // echo "Do something fancy" | crush run > output.txt
5965 //
60− // TODO: We currently need to press ^c twice to cancel. Fix that.
61− return app.RunNonInteractive(cmd.Context(), os.Stdout, prompt, quiet)
66+ return app.RunNonInteractive(ctx, os.Stdout, prompt, quiet)
6267 },
6368}
6469