// SPDX-License-Identifier: MIT package main import ( "errors" "os" "os/signal" "syscall" ) var errNoTerminal = errors.New("there is no terminal to ask for the password on: " + "give it on standard input with --password-stdin") // onInterrupt runs f if the program is interrupted before stop is called, // and then exits. It is there for the moment the terminal is not echoing: a // program ended while typing is hidden must not leave the terminal hiding it. func onInterrupt(f func()) (stop func()) { sig := make(chan os.Signal, 1) signal.Notify(sig, os.Interrupt, syscall.SIGTERM) done := make(chan struct{}) go func() { select { case <-sig: f() os.Exit(130) case <-done: } }() return func() { signal.Stop(sig) close(done) } }