go.mway.dev/x@v0.0.0-20240520034138-950aede9a3fb/os/write_file.go (about)

     1  // Copyright (c) 2024 Matt Way
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to
     5  // deal in the Software without restriction, including without limitation the
     6  // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
     7  // sell copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    18  // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    19  // IN THE THE SOFTWARE.
    20  
    21  package os
    22  
    23  import (
    24  	"io"
    25  	"os"
    26  
    27  	"go.mway.dev/errors"
    28  )
    29  
    30  const (
    31  	// DefaultWriteFlags are default flags used for writing to a file.
    32  	DefaultWriteFlags = os.O_CREATE | os.O_TRUNC | os.O_WRONLY
    33  
    34  	_fileMode = 0o644
    35  )
    36  
    37  var (
    38  	// ErrNilWriteReader indicates that a given reader value is nil or produced
    39  	// a nil reader.
    40  	ErrNilWriteReader = errors.New("nil write reader provided")
    41  	// ErrUnsupportedWriteReader indicates that a given value is not a
    42  	// supported write reader type.
    43  	ErrUnsupportedWriteReader = errors.New("unsupported write reader")
    44  )
    45  
    46  // ReaderFunc is a function that returns an [io.Reader] or an error.
    47  type ReaderFunc = func() (io.Reader, error)
    48  
    49  // ReadCloserFunc is a function that returns an [io.ReadCloser] or an error.
    50  type ReadCloserFunc = func() (io.ReadCloser, error)
    51  
    52  // WriteReaderToFileWithFlags creates path with flags and mode, and copies the
    53  // given reader to it.
    54  func WriteReaderToFileWithFlags(
    55  	path string,
    56  	reader any,
    57  	flags int,
    58  	mode os.FileMode,
    59  ) (written int, err error) {
    60  	var src io.Reader
    61  	switch t := reader.(type) {
    62  	case io.ReadCloser:
    63  		defer func() {
    64  			err = errors.Join(err, t.Close())
    65  		}()
    66  		src = t
    67  	case io.Reader:
    68  		src = t
    69  	case ReadCloserFunc:
    70  		var tmp io.ReadCloser
    71  		if tmp, err = t(); err != nil {
    72  			return 0, err
    73  		}
    74  		defer func() {
    75  			err = errors.Join(err, tmp.Close())
    76  		}()
    77  		src = tmp
    78  	case ReaderFunc:
    79  		if src, err = t(); err != nil {
    80  			return 0, err
    81  		}
    82  	default:
    83  		return 0, errors.Wrapf(ErrUnsupportedWriteReader, "%T", src)
    84  	}
    85  
    86  	var dst io.WriteCloser
    87  	if dst, err = os.OpenFile(path, flags, mode); err != nil {
    88  		return 0, err
    89  	}
    90  	defer func() {
    91  		err = errors.Join(err, dst.Close())
    92  	}()
    93  
    94  	var n int64
    95  	n, err = io.Copy(dst, src)
    96  	return int(n), err
    97  }
    98  
    99  // WriteReaderToFile creates path and copies the given reader to it.
   100  func WriteReaderToFile(path string, reader any) (written int, err error) {
   101  	return WriteReaderToFileWithFlags(
   102  		path,
   103  		reader,
   104  		DefaultWriteFlags,
   105  		_fileMode,
   106  	)
   107  }