diff --git a/buffer.go b/buffer.go new file mode 100644 index 0000000..65a88cb --- /dev/null +++ b/buffer.go @@ -0,0 +1,22 @@ +package scriptx + +import ( + "bytes" + "io" +) + +// Buffer will read the contents of the source in an internal buffer until an EOF is encountered. +// It will then write the entire contents of the buffer to the writer. Useful when reading and +// writing to the same file. +func Buffer() func(r io.Reader, w io.Writer) error { + return func(r io.Reader, w io.Writer) error { + var bfr bytes.Buffer + if _, err := io.Copy(&bfr, r); err != nil { + return err + } + if _, err := io.Copy(w, &bfr); err != nil { + return err + } + return nil + } +}