go.mway.dev/x@v0.0.0-20240520034138-950aede9a3fb/os/stream_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  	"errors"
    25  	"io"
    26  	"io/fs"
    27  	"os"
    28  )
    29  
    30  const _createFileFlags = os.O_CREATE | os.O_TRUNC | os.O_WRONLY
    31  
    32  // WithFileReader opens the file at path for reading and passes the file's
    33  // reader to fn, cleaning up once fn returns. WithFileReader returns any error
    34  // returned by fn as well as any other errors encountered.
    35  func WithFileReader(path string, fn func(r io.Reader) error) (err error) {
    36  	var src io.ReadCloser
    37  	if src, err = os.Open(path); err != nil {
    38  		return
    39  	}
    40  	defer func() {
    41  		err = errors.Join(err, src.Close())
    42  	}()
    43  
    44  	return fn(src)
    45  }
    46  
    47  // WithFileWriter opens the file at path for writing and passes the file's
    48  // writer to fn, cleaning up once fn returns. WithFileWriter returns any error
    49  // returned by fn as well as any other errors encountered.
    50  func WithFileWriter(path string, fn func(w io.Writer) error) (err error) {
    51  	return WithFileModeWriter(path, 0o744, fn)
    52  }
    53  
    54  // WithFileModeWriter opens (creating with the given mode or truncating, as
    55  // necessary) the file at path for writing and passes the file's writer to fn,
    56  // cleaning up once fn returns. WithFileModeWriter returns any error returned
    57  // by fn as well as any other errors encountered.
    58  func WithFileModeWriter(
    59  	path string,
    60  	mode fs.FileMode,
    61  	fn func(w io.Writer) error,
    62  ) (err error) {
    63  	var dst io.WriteCloser
    64  	if dst, err = os.OpenFile(path, _createFileFlags, mode); err != nil {
    65  		return
    66  	}
    67  	defer func() {
    68  		err = errors.Join(err, dst.Close())
    69  	}()
    70  
    71  	return fn(dst)
    72  }