From: Sergey Matveev Date: Thu, 15 Oct 2020 09:27:52 +0000 (+0300) Subject: Ability to read from stdin X-Git-Tag: v2.1.0^0 X-Git-Url: http://www.git.cypherpunks.ru/?p=netstring.git;a=commitdiff_plain;h=94ea20f86a2f66133023c9698e6ebfb67ea84e24 Ability to read from stdin --- diff --git a/cmd/netstring/main.go b/cmd/netstring/main.go index 306f46e..460341e 100644 --- a/cmd/netstring/main.go +++ b/cmd/netstring/main.go @@ -32,9 +32,21 @@ func usage() { fmt.Fprintf(os.Stderr, "Usage: %s list FILE\n", os.Args[0]) fmt.Fprintf(os.Stderr, " %s read FILE CHUNK > data\n", os.Args[0]) fmt.Fprintf(os.Stderr, " %s write FILE ... > data\n", os.Args[0]) + fmt.Fprintf(os.Stderr, "FILE can be \"-\" for reading from stdin\n") os.Exit(1) } +func fileRead(path string) io.Reader { + if path == "-" { + return os.Stdin + } + fd, err := os.Open(path) + if err != nil { + panic(err) + } + return fd +} + func main() { flag.Usage = usage flag.Parse() @@ -43,11 +55,7 @@ func main() { } switch os.Args[1] { case "list": - fd, err := os.Open(os.Args[2]) - if err != nil { - panic(err) - } - r := netstring.NewReader(fd) + r := netstring.NewReader(fileRead(os.Args[2])) for i := 0; ; i++ { size, err := r.Next() if err == io.EOF { @@ -67,11 +75,7 @@ func main() { if err != nil { panic(err) } - fd, err := os.Open(os.Args[2]) - if err != nil { - panic(err) - } - r := netstring.NewReader(fd) + r := netstring.NewReader(fileRead(os.Args[2])) for i := 0; i < chunk; i++ { _, err = r.Next() if err != nil {