Files
magpie/internal/cli/signals.go
T
yetone 23e764d0e0 cli: magpie's commands move from the root into internal/cli; the root keeps only main.go
The root held 89 .go files (30 commands, 59 tests). They move as they are
to internal/cli (package cli), with git mv so history follows them. The
only code change is main() becoming cli.Main(version): the root's new
main.go keeps `var version` in package main, so -ldflags "-X
main.version=…" in the Makefile, the Dockerfile and magpie-releases, and
every workflow, work unchanged. go-winres's rsrc_windows_*.syso still land
next to package main in the root.

Tests that read testdata by a root-relative path now go up one level
(../plugin/testdata/fake, ../sessions/testdata). Five subsystem doc links
point at internal/cli/.

Run:
- go build ./..., gofmt -l, go vet on darwin, linux and windows (nogui)
- builds with tags production, dev, "production nogui"; windows
  -H windowsgui; linux CGO_ENABLED=0
- make release-cli TARGETS="darwin/arm64 linux/amd64 windows/amd64
  android/arm64" VERSION=9.9.9-tidy: all four built, binary prints
  "magpie 9.9.9-tidy"
- docker build --build-arg VERSION=7.7.7-tidy: container prints
  "magpie 7.7.7-tidy"
- go test ./internal/cli (default and nogui): 152 tests, the same count
  as the root on main
- go test ./...: all packages pass (agent, gateway, library, provider
  timed out at 10m under other sessions' load and passed rerun alone
  with -timeout 40m; none of them changed)
- the built binary in a sandbox HOME: version, help, ls, providers,
  claude, and an unknown command's error
2026-10-12 20:40:22 +08:00

102 lines
3.1 KiB
Go

package cli
import (
"context"
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"
"github.com/yetone/magpie/internal/proc"
)
// endProbesOnSignal: the probes magpie asks a CLI with lead a session of
// their own (proc.ProbeContext), so Ctrl+C in the terminal, the terminal
// closing or a service manager's SIGTERM reach magpie and not them, and
// magpie ending on the signal left them to init. They are ended first, and
// magpie then dies of the signal. A command that waits on Ctrl+C itself
// (interruptContext) is left to finish its own way, its probes ended all the
// same; the app and the TUI, which quit on SIGINT and SIGTERM their own way,
// take those back (ownSignals). A signal magpie was started ignoring (nohup,
// a background job) stays ignored: asking for it would undo that.
func endProbesOnSignal() {
caught = nil
for _, s := range []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGHUP} {
if !signal.Ignored(s) {
caught = append(caught, s)
}
}
if len(caught) == 0 {
return
}
onSignal = make(chan os.Signal, 1)
signal.Notify(onSignal, caught...)
go func(sigs chan os.Signal) {
for s := range sigs {
// asked before the probes are ended, which can take a moment
// the command may spend finishing, and stop asking
handled := s == os.Interrupt && interrupts.Load() > 0
proc.EndProbes()
if !handled {
die(s)
}
}
}(onSignal)
}
// caught are the signals endProbesOnSignal asked for; onSignal is where
// they come.
var (
caught []os.Signal
onSignal chan os.Signal
)
// die ends magpie of s, as s would have had there been no handler: the
// shell that ran it sees a process a signal ended (a loop running magpie
// stops on Ctrl+C), not one that exited. Where a signal can't be sent to
// oneself (Windows), it exits as a shell reports such a process. A var so
// tests can see what a signal would do.
var die = func(s os.Signal) {
raise(s)
code := 1
if n, ok := s.(syscall.Signal); ok {
code = 128 + int(n)
}
os.Exit(code)
}
// ownSignals hands SIGINT and SIGTERM back, for a command that quits on
// them its own way: Wails ends the app through OnShutdown (which ends the
// probes, and installs a downloaded update), bubbletea leaves the TUI's
// screen as it found it, and each comes back to main, where the probes are
// ended too. Exiting first skipped all of that. SIGHUP stays: neither
// handles it, and the terminal closing would end magpie there and then,
// with its probes, out of the terminal's reach, left running.
func ownSignals() {
if onSignal == nil {
return
}
signal.Stop(onSignal)
for _, s := range caught {
if s == syscall.SIGHUP {
signal.Notify(onSignal, s)
}
}
}
// interrupts counts the commands ending themselves on Ctrl+C.
var interrupts atomic.Int32
// interruptContext is signal.NotifyContext(…, os.Interrupt), for a command
// that ends itself on Ctrl+C rather than being ended by it.
func interruptContext() (context.Context, context.CancelFunc) {
interrupts.Add(1)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
var once sync.Once
return ctx, func() {
stop()
once.Do(func() { interrupts.Add(-1) })
}
}