Documentation
¶
Overview ¶
Package ioz contains supplemental io functionality.
Index ¶
- Constants
- func Close(ctx context.Context, c io.Closer)
- func CopyAsync(w io.Writer, r io.Reader, callback func(written int64, err error))
- func CopyFile(dst, src string, mkdir bool) error
- func DelayReader(r io.Reader, delay time.Duration, jitter bool) io.Reader
- func DirExists(dir string) bool
- func DirSize(path string) (int64, error)
- func DrainClose(rc io.ReadCloser) (n int, err error)
- func FPrintFile(w io.Writer, name string) error
- func FileAccessible(path string) bool
- func FileInfoEqual(a, b os.FileInfo) bool
- func Filesize(fp string) (int64, error)
- func IsPathToRegularFile(path string) bool
- func LimitRandReader(limit int64) io.Reader
- func MarshalYAML(v any) ([]byte, error)
- func NewErrorAfterNReader(n int, err error) io.Reader
- func NotifyOnEOFReader(r io.Reader, fn func(error) error) io.Reader
- func NotifyOnErrorReader(r io.Reader, fn func(error) error) io.Reader
- func NotifyOnceWriter(w io.Writer, fn func()) io.Writer
- func PrintFile(name string) error
- func PrintTree(w io.Writer, loc string, showSize, colorize bool) error
- func PruneEmptyDirTree(ctx context.Context, dir string) (count int, err error)
- func ReadCloserNotifier(rc io.ReadCloser, fn func(closeErr error)) io.ReadCloser
- func ReadDir(dir string, includeDirPath, markDirs, includeDot bool) (paths []string, err error)
- func ReadFileToString(name string) (string, error)
- func RenameDir(oldDir, newpath string) error
- func RequireDir(dir string) error
- func StartMemStatsTracker(ctx context.Context, sampleFreq time.Duration) (sys *atomic.Uint64, allocs, gcPauseNs *atomic.Uint64)
- func UnmarshallYAML(data []byte, v any) error
- func WriteCloser(w io.Writer) io.WriteCloser
- func WriteToFile(ctx context.Context, fp string, r io.Reader) (written int64, err error)
- type WriteErrorCloser
Constants ¶
const RWPerms = os.FileMode(0o600)
RWPerms is the default file mode used for creating files.
Variables ¶
This section is empty.
Functions ¶
func Close ¶ added in v0.36.0
Close is a convenience function to close c, logging a warning if c.Close returns an error. This is useful in defer, e.g.
defer ioz.Close(ctx, c)
func CopyAsync ¶ added in v0.47.0
CopyAsync asynchronously copies from r to w, invoking non-nil callback when done.
func CopyFile ¶ added in v0.47.0
CopyFile copies the contents from src to dst atomically. If dst does not exist, CopyFile creates it with src's perms. If the copy fails, CopyFile aborts and dst is preserved. If mkdir is true, the directory for dst is created if it doesn't exist.
func DelayReader ¶ added in v0.47.0
DelayReader returns an io.Reader that delays on each read from r. This is primarily intended for testing. If jitter is true, a randomized jitter factor is added to the delay. If r implements io.Closer, the returned reader will also implement io.Closer; if r doesn't implement io.Closer, the returned reader will not implement io.Closer. If r is nil, nil is returned.
func DrainClose ¶ added in v0.47.0
func DrainClose(rc io.ReadCloser) (n int, err error)
DrainClose drains rc, returning the number of bytes read, and any error. The reader is always closed, even if the drain operation returned an error. If both the drain and the close operations return non-nil errors, the drain error is returned.
func FPrintFile ¶
FPrintFile reads file from name and writes it to w.
func FileAccessible ¶ added in v0.47.0
FileAccessible returns true if path is a file that can be read.
func FileInfoEqual ¶ added in v0.47.0
FileInfoEqual returns true if a and b are equal. The FileInfo.Sys() field is ignored.
func Filesize ¶ added in v0.47.0
Filesize returns the size of the file at fp. An error is returned if fp doesn't exist or is a directory.
func IsPathToRegularFile ¶ added in v0.37.0
IsPathToRegularFile return true if path is a regular file or a symlink that resolves to a regular file. False is returned on any error.
func LimitRandReader ¶ added in v0.47.0
LimitRandReader returns an io.Reader that reads up to limit bytes from crypto/rand.Reader.
func MarshalYAML ¶
MarshalYAML is our standard mechanism for encoding YAML.
func NewErrorAfterNReader ¶ added in v0.47.0
NewErrorAfterNReader returns an io.Reader that returns err after reading n random bytes from crypto/rand.Reader.
func NotifyOnEOFReader ¶ added in v0.47.0
NotifyOnEOFReader returns an io.Reader that invokes fn when r.Read returns io.EOF. The error that fn returns is what's returned to the r caller: fn can transform the error or return it unchanged. If r or fn is nil, r is returned.
If r is an io.ReadCloser, the returned reader will also implement io.ReadCloser.
See also: NotifyOnErrorReader, which is a generalization of NotifyOnEOFReader.
func NotifyOnErrorReader ¶ added in v0.47.0
NotifyOnErrorReader returns an io.Reader that invokes fn when r.Read returns an error. The error that fn returns is what's returned to the r caller: fn can transform the error or return it unchanged. If r or fn is nil, r is returned.
See also: NotifyOnEOFReader, which is a specialization of NotifyOnErrorReader.
func NotifyOnceWriter ¶ added in v0.47.0
NotifyOnceWriter returns an io.Writer that invokes fn on the first invocation of Write. If w or fn is nil, w is returned.
func PrintTree ¶ added in v0.47.0
PrintTree prints the file tree structure at loc to w. This function uses the github.com/a8m/tree library, which is a Go implementation of the venerable "tree" command.
func PruneEmptyDirTree ¶ added in v0.47.0
PruneEmptyDirTree prunes empty dirs, and dirs that contain only other empty dirs, from the directory tree rooted at dir. If a dir contains at least one non-dir entry, that dir is spared. Arg dir must be an absolute path.
func ReadCloserNotifier ¶ added in v0.47.0
func ReadCloserNotifier(rc io.ReadCloser, fn func(closeErr error)) io.ReadCloser
ReadCloserNotifier returns a new io.ReadCloser that invokes fn after Close is called, passing along any error from Close. If rc or fn is nil, rc is returned. Note that any subsequent calls to Close are no-op, and return the same error (if any) as the first invocation of Close.
func ReadDir ¶ added in v0.37.0
ReadDir lists the contents of dir, returning the relative paths of the files. If markDirs is true, directories are listed with a "/" suffix (including symlinked dirs). If includeDirPath is true, the listing is of the form "dir/name". If includeDot is true, files beginning with period (dot files) are included. The function attempts to continue in the present of errors: the returned paths may contain values even in the presence of a returned error (which may be a multierr).
func ReadFileToString ¶ added in v0.47.0
ReadFileToString reads the file at name and returns its contents as a string.
func RenameDir ¶ added in v0.47.0
RenameDir is like os.Rename, but it works even if newpath already exists and is a directory (which os.Rename fails on).
func RequireDir ¶ added in v0.47.0
RequireDir ensures that dir exists and is a directory, creating it if necessary.
func StartMemStatsTracker ¶ added in v0.47.0
func StartMemStatsTracker(ctx context.Context, sampleFreq time.Duration) (sys *atomic.Uint64, allocs, gcPauseNs *atomic.Uint64, )
StartMemStatsTracker starts a goroutine that tracks memory stats, returning the peak values of runtime.MemStats.Sys, runtime.MemStats.TotalAlloc and runtime.MemStats.PauseTotalNs. The goroutine sleeps for sampleFreq between each sample and exits when ctx is done.
func UnmarshallYAML ¶
UnmarshallYAML is our standard mechanism for decoding YAML.
func WriteCloser ¶ added in v0.47.0
func WriteCloser(w io.Writer) io.WriteCloser
WriteCloser returns w as an io.WriteCloser. If w implements io.WriteCloser, w is returned. Otherwise, w is wrapped in a no-op decorator that implements io.WriteCloser.
WriteCloser is the missing sibling of io.NopCloser, which isn't implemented in stdlib. See: https://github.com/golang/go/issues/22823.
func WriteToFile ¶ added in v0.47.0
WriteToFile writes the contents of r to fp. If fp doesn't exist, the file is created (including any parent dirs). If fp exists, it is truncated. The write operation is context-aware.
Types ¶
type WriteErrorCloser ¶ added in v0.47.0
type WriteErrorCloser interface { io.WriteCloser // Error indicates that an upstream error has interrupted the // writing operation. Error(err error) }
WriteErrorCloser supplements io.WriteCloser with an Error method, indicating to the io.WriteCloser that an upstream error has interrupted the writing operation. Note that clients should invoke only one of Close or Error.
func NewFuncWriteErrorCloser ¶ added in v0.47.0
func NewFuncWriteErrorCloser(w io.WriteCloser, fn func(error)) WriteErrorCloser
NewFuncWriteErrorCloser returns a new WriteErrorCloser that wraps w, and invokes non-nil fn when WriteErrorCloser.Error is called.
Directories
¶
Path | Synopsis |
---|---|
Package checksum provides functions for working with checksums.
|
Package checksum provides functions for working with checksums. |
Package contextio provides io decorators that are context-aware.
|
Package contextio provides io decorators that are context-aware. |
Package httpz provides functionality supplemental to stdlib http.
|
Package httpz provides functionality supplemental to stdlib http. |
Package lockfile implements a pid lock file mechanism.
|
Package lockfile implements a pid lock file mechanism. |