From 16bf5ace2a3b95a990978490cad044c066b4d47a Mon Sep 17 00:00:00 2001 From: Blallo Date: Thu, 30 Apr 2020 14:04:01 +0200 Subject: [PATCH] Add timeout for reading from stdin --- main.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index b38a52b..9152fd3 100644 --- a/main.go +++ b/main.go @@ -6,12 +6,13 @@ import ( "fmt" "os" "strings" + "time" ) var noVersion = "dev" var version string -func readFromConsole() string { +func readFromConsole(result chan string) { var text, line string var err error counter := 0 @@ -29,7 +30,8 @@ func readFromConsole() string { Error.F("Error in reading text from console\n%s", err) os.Exit(1) } - return strings.TrimRight(text, "\n") + result <- strings.TrimRight(text, "\n") + return } func toList(input string) []string { @@ -100,7 +102,16 @@ func main() { } if flag.NArg() == 0 { - text = readFromConsole() + result := make(chan string, 1) + go readFromConsole(result) + select { + case text = <-result: + Info.Ln("text read from console") + case <-time.After(5 * time.Minute): + Error.Ln("timeout reading from console") + os.Exit(3) + } + } else { for _, arg := range flag.Args() { text += fmt.Sprintf("%s\n", arg)