Version: v1.8.75 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2021 License: MIT Imports: 30 Imported by: 0



Helpers for working with files and filesystems



View Source
const DefaultOpenTimeout = time.Duration(10 * time.Second)


View Source
var CompareHasher hash.Hash = sha256.New()
View Source
var DirExists = pathutil.DirExists

Alias functions from pathutil as a convenience

View Source
var Exists = pathutil.Exists
View Source
var ExpandUser = pathutil.ExpandUser
View Source
var FileExists = pathutil.FileExists
View Source
var IsAppend = pathutil.IsAppend
View Source
var IsAppendable = pathutil.IsAppendable
View Source
var IsCharDevice = pathutil.IsCharDevice
View Source
var IsDevice = pathutil.IsDevice
View Source
var IsExclusive = pathutil.IsExclusive
View Source
var IsNamedPipe = pathutil.IsNamedPipe
View Source
var IsNonemptyDir = pathutil.IsNonemptyDir
View Source
var IsNonemptyExecutableFile = pathutil.IsNonemptyExecutableFile
View Source
var IsNonemptyFile = pathutil.IsNonemptyFile
View Source
var IsReadable = pathutil.IsReadable
View Source
var IsSetgid = pathutil.IsSetgid
View Source
var IsSetuid = pathutil.IsSetuid
View Source
var IsSocket = pathutil.IsSocket
View Source
var IsSticky = pathutil.IsSticky
View Source
var IsSymlink = pathutil.IsSymlink
View Source
var IsTemporary = pathutil.IsTemporary
View Source
var IsWritable = pathutil.IsWritable
View Source
var LinkExists = pathutil.LinkExists
View Source
var SkipToken = errors.New(`skip token`)

By default, the underlying bufio.Scanner that tokenizes the input data will discard the tokens that it's splitting on. however, in most cases, we don't actually want this. however, if the thing we're removing IS this token, then we will have removed the token, then immediately put it back in the stream.

Returning the SkipToken error will tell the ReadManipulator to not put this token back into the stream, but otherwise not produce an actual error during read.


func AddMimeTypeFile

func AddMimeTypeFile(filename string)

func ChecksumFile

func ChecksumFile(filename string, fn interface{}) ([]byte, error)

func CompareReaders

func CompareReaders(a io.Reader, b io.Reader, hasher ...hash.Hash) int

Compare the binary contents of two io.Reader instances. The result will be 0 if a==b, -1 if a < b, and +1 if a > b. An optional hash.Hash instance may be given, otherwise the default crypto/sha256 will be used.

func CopyDir

func CopyDir(root string, fn CopyEntryFunc) error

Recursively walk the entries of a given directory, calling CopyEntryFunc for each entry. The io.Writer returned from the function will have that file's contents written to it. If the io.Writer is nil, the file will not be written anywhere but no error will be returned. If CopyEntryFunc returns an error, the behavior will be consistent with filepath.WalkFunc

func CopyFile

func CopyFile(source interface{}, destination interface{}) error

Copy a file from one place to another. Source can be an io.Reader or string. If source is a string, the string will be passed to the Open() function as a URL. Destination can be an io.Writer or string. If destination is a string, it will be treated as a local filesystem path to write the data read from source to.

If either source or destination implements io.Closer, thee files will be closed before this function returns.

func GetMimeType

func GetMimeType(filenameOrReader interface{}, fallback ...string) string

GetMimeType goes out of its way to really, really try to figure out the MIME type of a given filename or io.Reader. If the first argument satisfies the io.Seeker interface, the seeker will Seek() back to the beginning. If it satisfies the io.Closer interface, it will be closed.

func GetNthLine

func GetNthLine(file interface{}, number int) (string, error)

Attempt to return the nth line (starting from 1) in the given file or reader.

func InitMime

func InitMime()

func IsModifiedAfter

func IsModifiedAfter(stat os.FileInfo, current string) bool

Return true if the given FileInfo sports a ModTime later than the current file.

func IsTerminal

func IsTerminal() bool

func MustExpandUser

func MustExpandUser(path string) string

func MustGetNthLine

func MustGetNthLine(file interface{}, number int) string

A panicky version of GetNthLine.

func MustReadAll

func MustReadAll(file interface{}) []byte

func MustReadAllLines

func MustReadAllLines(file interface{}) []string

A panicky version of ReadAllLines.

func MustReadAllString

func MustReadAllString(file interface{}) string

func MustWriteFile

func MustWriteFile(input interface{}, filename string) int64

Same as WriteFile, but will panic if the file cannot be written.

func MustWriteTempFile

func MustWriteTempFile(input interface{}, prefix string) string

Same as MustWriteFile, but writes the given input to a temporary file, returning the filename. The function will panic if the file cannot be written.

func Open

func Open(uri string) (io.Reader, error)

Calls OpenWithOptions with no options set.

func OpenWithOptions

func OpenWithOptions(uri string, options OpenOptions) (io.Reader, error)

A generic URL opener that supports various schemes and returns an io.Reader. Supported URL schemes include: file://, http://, https://, ftp://, sftp://. If no scheme is provided, the URL is interpreted as a local filesystem path.

func ReadAll

func ReadAll(file interface{}) ([]byte, error)

Takes the given string filename, []byte, io.Reader, or io.ReadCloser and returns the bytes therein.

func ReadAllLines

func ReadAllLines(file interface{}) ([]string, error)

Read all lines of text from the given file and return them as a slice.

func ReadAllString

func ReadAllString(file interface{}) (string, error)

A string version of ReadAll.

func ReadFirstLine

func ReadFirstLine(file interface{}) (string, error)

func RegisterOpenHandler

func RegisterOpenHandler(scheme string, handler OpenHandler)

Register a handler for a new or existing URL scheme, for use with Open() and OpenWithOptions()

func RemoveBlankLines

func RemoveBlankLines(data []byte) ([]byte, error)

A ReadManipulatorFunc for removing lines that only contain whitespace.

func RemoveOpenHandler

func RemoveOpenHandler(scheme string)

Removes a registered URL scheme handler.

func SameFile

func SameFile(first interface{}, second interface{}) bool

Returns whether two files represent the same file. If a string is given for either file, os.Stat will be run on that path. If os.FileInfo is given for either file, it will be passed to os.SameFile directly. If either file is an io.Reader, the contents of both files will be read and hashed using CompareHasher. If the hashes are identical, the files are considered the same. Any error encountered and this function will return false.

func SetExt

func SetExt(path string, ext string, oldexts ...string) string

Detects the file extension of the given path and replaces it with the given extension. The optional second argument allows you to explictly specify the extension (if known).

func ShouldGetNthLine

func ShouldGetNthLine(file interface{}, number int) string

Attempts to call GetNthLine, but will return an empty string if there is an error. Does not panic.

func ShouldReadAllLines

func ShouldReadAllLines(file interface{}) []string

Attempts to call ReadAllLines, but will return an empty slice if there is an error. Does not panic.

func SizeOf

func SizeOf(path string) convutil.Bytes

Returns the size of the given filename, or zero if any error is encountered (or if the file is zero length).

func WriteFile

func WriteFile(input interface{}, filename string) (int64, error)

Write the contents of the given io.Reader, []byte, or string to the specified filename. Filename paths containing tilde (~) will automatically expand to the current user's home directory, and all intermediate parent directories will be automatically created. Will return the number of bytes written, or an error.

func WriteTempFile

func WriteTempFile(input interface{}, pattern string) (string, error)

Same as WriteFile, but writes the given input to a temporary file, returning the filename.


type CopyEntryFunc

type CopyEntryFunc func(path string, info os.FileInfo, err error) (io.Writer, error)

type DirReader

type DirReader struct {
	// contains filtered or unexported fields

A DirReader provides a streaming io.Reader interface to all files in a given directory, with options for handling unreadable entries and recursion.

func NewDirReader

func NewDirReader(path string, options ...DirReaderOption) *DirReader

func (*DirReader) Close

func (self *DirReader) Close() error

close open files and reset the internal reader

func (*DirReader) Read

func (self *DirReader) Read(b []byte) (int, error)

func (*DirReader) SetSkipFunc

func (self *DirReader) SetSkipFunc(fn SkipFunc)

Set a function that will be called for each path encountered while reading. If this function returns true, that path (and its descedants) will not be read.

type DirReaderOption

type DirReaderOption int
const (
	NoRecursive DirReaderOption = iota

type DirReaderOptions

type DirReaderOptions []DirReaderOption

func (DirReaderOptions) Has

func (self DirReaderOptions) Has(option DirReaderOption) bool

type FileInfo

type FileInfo struct {
	// contains filtered or unexported fields

An os.FileInfo-compatible wrapper that allows for individual values to be overridden.

func NewFileInfo

func NewFileInfo(wrap ...os.FileInfo) *FileInfo

func (*FileInfo) IsDir

func (self *FileInfo) IsDir() bool

func (*FileInfo) ModTime

func (self *FileInfo) ModTime() time.Time

func (*FileInfo) Mode

func (self *FileInfo) Mode() os.FileMode

func (*FileInfo) Name

func (self *FileInfo) Name() string

func (*FileInfo) SetIsDir

func (self *FileInfo) SetIsDir(isDir bool)

func (*FileInfo) SetModTime

func (self *FileInfo) SetModTime(mtime time.Time)

func (*FileInfo) SetMode

func (self *FileInfo) SetMode(mode os.FileMode)

func (*FileInfo) SetName

func (self *FileInfo) SetName(name string)

func (*FileInfo) SetSize

func (self *FileInfo) SetSize(sz int64)

func (*FileInfo) SetSys

func (self *FileInfo) SetSys(iface interface{})

func (*FileInfo) Size

func (self *FileInfo) Size() int64

func (*FileInfo) Sys

func (self *FileInfo) Sys() interface{}

type OpenHandler

type OpenHandler func(*url.URL, OpenOptions) (io.ReadCloser, error)

type OpenOptions

type OpenOptions struct {
	Timeout  time.Duration
	Insecure bool

func (OpenOptions) GetTimeout

func (self OpenOptions) GetTimeout() time.Duration

type PostReadCloser

type PostReadCloser struct {
	// contains filtered or unexported fields

func NewPostReadCloser

func NewPostReadCloser(upstream io.ReadCloser, closer func(io.ReadCloser) error) *PostReadCloser

Implements an io.ReadCloser that can be configured to perform cleanup options whenever the Close() function is called.

func (*PostReadCloser) Close

func (self *PostReadCloser) Close() error

func (*PostReadCloser) Read

func (self *PostReadCloser) Read(b []byte) (int, error)

type ReadManipulator

type ReadManipulator struct {
	// contains filtered or unexported fields

func NewReadManipulator

func NewReadManipulator(reader io.Reader, fns ...ReadManipulatorFunc) *ReadManipulator

func (*ReadManipulator) Close

func (self *ReadManipulator) Close() error

func (*ReadManipulator) Read

func (self *ReadManipulator) Read(b []byte) (int, error)

func (*ReadManipulator) Split

func (self *ReadManipulator) Split(split bufio.SplitFunc)

type ReadManipulatorFunc

type ReadManipulatorFunc func(data []byte) ([]byte, error)

func ManipulateAll

func ManipulateAll(fns ...ReadManipulatorFunc) ReadManipulatorFunc

Performs multiple sequential manipulations on an intercepted line of text from an io.Reader as its being read.

func RemoveLinesContaining

func RemoveLinesContaining(needle string) ReadManipulatorFunc

A ReadManipulatorFunc for removing lines that contain the given string.

func RemoveLinesSurroundedBy

func RemoveLinesSurroundedBy(prefix string, suffix string, trimSpace bool) ReadManipulatorFunc

A ReadManipulatorFunc for removing lines surrounded by a given prefix and suffix.

func RemoveLinesWithPrefix

func RemoveLinesWithPrefix(prefix string, trimSpace bool) ReadManipulatorFunc

A ReadManipulatorFunc for removing lines that have a given prefix.

func RemoveLinesWithSuffix

func RemoveLinesWithSuffix(suffix string, trimSpace bool) ReadManipulatorFunc

A ReadManipulatorFunc for removing lines that have a given suffix.

func ReplaceWith

func ReplaceWith(find string, replace string, occurrences int) ReadManipulatorFunc

A ReadManipulatorFunc for replacing text in an io.Reader as its being read.

type RewriteFileSystem

type RewriteFileSystem struct {
	FileSystem http.FileSystem
	Find       *regexp.Regexp
	Replace    string
	MustMatch  bool

func (RewriteFileSystem) Open

func (self RewriteFileSystem) Open(name string) (http.File, error)

type SkipFunc

type SkipFunc func(string) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL