Skip to content

Commit f3fd256

Browse files
committed
fix: Restore terminal on interrupt when connecting
Fixes #1292.
1 parent 568574c commit f3fd256

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

cli/cliui/agent.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"os"
8+
"os/signal"
79
"sync"
810
"time"
911

@@ -46,6 +48,22 @@ func Agent(ctx context.Context, writer io.Writer, opts AgentOptions) error {
4648
spin.Start()
4749
defer spin.Stop()
4850

51+
ctx, cancelFunc := context.WithCancel(ctx)
52+
defer cancelFunc()
53+
stopSpin := make(chan os.Signal, 1)
54+
signal.Notify(stopSpin, os.Interrupt)
55+
defer signal.Stop(stopSpin)
56+
go func() {
57+
select {
58+
case <-ctx.Done():
59+
return
60+
case <-stopSpin:
61+
}
62+
signal.Stop(stopSpin)
63+
spin.Stop()
64+
os.Exit(1)
65+
}()
66+
4967
ticker := time.NewTicker(opts.FetchInterval)
5068
defer ticker.Stop()
5169
timer := time.NewTimer(opts.WarnInterval)

0 commit comments

Comments
 (0)