github.com/puellanivis/breton@v0.2.16/lib/files/write.go (about)

     1  package files
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  )
     7  
     8  // WriteTo writes the entire content of data to an io.Writer.
     9  // If the Writer also implements io.Closer, it will also Close it.
    10  func WriteTo(w io.Writer, data []byte) error {
    11  	n, err := w.Write(data)
    12  
    13  	if err == nil && n < len(data) {
    14  		err = io.ErrShortWrite
    15  	}
    16  
    17  	if c, ok := w.(io.Closer); ok {
    18  		if err2 := c.Close(); err == nil {
    19  			err = err2
    20  		}
    21  	}
    22  
    23  	return err
    24  }
    25  
    26  // Write writes the entire content of data to the resource at the given URL.
    27  func Write(ctx context.Context, url string, data []byte) error {
    28  	f, err := Create(ctx, url)
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	return WriteTo(f, data)
    34  }