diff options
| author | tsne <tsne.dev@outlook.com> | 2026-07-06 16:52:20 +0200 |
|---|---|---|
| committer | tsne <tsne.dev@outlook.com> | 2026-07-06 21:37:42 +0200 |
| commit | 84da43998f7cc42cc72652333ac6e3d293abb23e (patch) | |
| tree | 6610f82df854ae86f7f34152bb05fc712ef6f314 /cmd | |
| download | hopper-84da43998f7cc42cc72652333ac6e3d293abb23e.tar.gz | |
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/hopper/main.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/cmd/hopper/main.go b/cmd/hopper/main.go new file mode 100644 index 0000000..cc58d0d --- /dev/null +++ b/cmd/hopper/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "context" + "os" + "os/signal" + "strconv" + "sync/atomic" + "syscall" + + "tsne.dev/hopper/internal/cli" + "tsne.dev/hopper/internal/log" +) + +func main() { + log.Init(os.Stdout, os.Stderr) + + sigch := make(chan os.Signal, 1) + signal.Notify(sigch, syscall.SIGINT, syscall.SIGTERM) + + ctx, cancel := context.WithCancelCause(context.Background()) + defer cancel(nil) + + var receivedSignal int64 + go func() { + sig := (<-sigch).(syscall.Signal) + atomic.StoreInt64(&receivedSignal, int64(sig)) + + var signame string + switch sig { + case syscall.SIGINT: + signame = "SIGINT" + case syscall.SIGTERM: + signame = "SIGTERM" + default: + signame = "signal " + strconv.Itoa(int(sig)) + } + + log.Print("received %s", signame) + cancel(cli.CancellationError("operation aborted (" + signame + ")")) + }() + + code := cli.Run(ctx, os.Args[1:]) + if sig := atomic.LoadInt64(&receivedSignal); sig != 0 { + code = 128 + int(sig) + } + os.Exit(code) +} |