From cf52568656a809d20b1719cf1729ddd7c91d9339 Mon Sep 17 00:00:00 2001 From: Dakota Walsh Date: Sat, 15 Jan 2022 20:34:39 +1300 Subject: [PATCH] implement io.ReadAll for older go versions --- main.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index c2d3d24..a5c1e73 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,7 @@ func main() { if len(args) == 0 { // use stdin data - inBytes, err := io.ReadAll(os.Stdin) + inBytes, err := ReadAll(os.Stdin) if err != nil { fmt.Fprintf(os.Stderr, "pcf: failed reading stdin: %v\n", err) os.Exit(1) @@ -121,3 +121,23 @@ func exit(c *ftp.ServerConn) { fmt.Fprintf(os.Stderr, "pcf: quit: %v\n", err) } } + +// ReadAll is from go 1.16 +// Implementing it ourself allows building in older go versions. +func ReadAll(r io.Reader) ([]byte, error) { + b := make([]byte, 0, 512) + for { + if len(b) == cap(b) { + // Add more capacity (let append pick how much). + b = append(b, 0)[:len(b)] + } + n, err := r.Read(b[len(b):cap(b)]) + b = b[:len(b)+n] + if err != nil { + if err == io.EOF { + err = nil + } + return b, err + } + } +} -- 2.45.2