Last active
March 19, 2019 16:26
-
-
Save montanaflynn/5ae3eeae7212b0ba232f46e88f1ab67f to your computer and use it in GitHub Desktop.
Watch for signals in terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"os/signal" | |
"syscall" | |
"golang.org/x/crypto/ssh/terminal" | |
) | |
func main() { | |
// check for ctrl-c signal | |
signalChan := make(chan os.Signal) | |
signal.Notify(signalChan, os.Interrupt) | |
go func() { | |
<-signalChan | |
os.Exit(1) | |
}() | |
// read password | |
p, err := terminal.ReadPassword(syscall.Stdin) | |
if err != nil { | |
panic(err) | |
} | |
// stop watching signals | |
signal.Stop(signalChan) | |
// print password | |
fmt.Println(string(p)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, thanks for this.
I gave it a try, unfortunately it still left my terminal in a bad state after issuing a Crtl+C while the prompt is open.
I was able to fix it by adding/amending the following: