~kota/pcf

cf52568656a809d20b1719cf1729ddd7c91d9339 — Dakota Walsh 5 months ago 273b271
implement io.ReadAll for older go versions
1 files changed, 21 insertions(+), 1 deletions(-)

M main.go
M main.go => main.go +21 -1
@@ 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
		}
	}
}