github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/utils/iohelp/write.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package iohelp 16 17 import ( 18 "encoding/binary" 19 "io" 20 ) 21 22 // WriteIfNoErr allows you to chain calls to write and handle errors at the very end. If the error passed into the 23 // the function is non-nil then the error will be returned without any io. If the error is nil then all bytes in the 24 // supplied buffer will be written to the io.Writer 25 func WriteIfNoErr(w io.Writer, bytes []byte, err error) error { 26 if err != nil { 27 return err 28 } 29 30 return WriteAll(w, bytes) 31 } 32 33 // WritePrimIfNoErr allows you to chain calls to write and handle errors at the very end. If the error passed into the 34 // function is non-nil then the error will be returned without any io. If the error is nil then supplied primitive will 35 // be written to the io.Writer using binary.Write with BigEndian byte ordering 36 func WritePrimIfNoErr(w io.Writer, prim interface{}, err error) error { 37 if err != nil { 38 return err 39 } 40 41 return binary.Write(w, binary.BigEndian, prim) 42 } 43 44 // WriteAll will write the entirety of the supplied data buffers to an io.Writer. This my result in multiple calls to 45 // the io.Writer's Write method in order to write the entire buffer, and if at any point there is an error then 46 // the error will be returned. 47 func WriteAll(w io.Writer, dataBuffers ...[]byte) error { 48 for _, data := range dataBuffers { 49 dataSize := len(data) 50 for written := 0; written < dataSize; { 51 n, err := w.Write(data[written:]) 52 53 if err != nil { 54 return err 55 } 56 57 written += n 58 } 59 } 60 61 return nil 62 } 63 64 var newLineBuf = []byte("\n") 65 66 // WriteLine will write the given string to an io.Writer followed by a newline. 67 func WriteLine(w io.Writer, line string) error { 68 return WriteAll(w, []byte(line), newLineBuf) 69 } 70 71 // WriteLines will write the given strings to an io.Writer, each followed by a newline. 72 func WriteLines(w io.Writer, lines ...string) error { 73 for _, line := range lines { 74 err := WriteLine(w, line) 75 if err != nil { 76 return err 77 } 78 } 79 return nil 80 } 81 82 type nopWrCloser struct { 83 io.Writer 84 } 85 86 func (nopWrCloser) Close() error { return nil } 87 88 // NopWrCloser returns a WriteCloser with a no-op Close method wrapping the provided Writer wr. 89 func NopWrCloser(wr io.Writer) io.WriteCloser { 90 return nopWrCloser{wr} 91 }