Documentation ¶
Overview ¶
Helpers for working with files and filesystems
Index ¶
- Constants
- Variables
- func AddMimeTypeFile(filename string)
- func ChecksumFile(filename string, fn interface{}) ([]byte, error)
- func CompareReaders(a io.Reader, b io.Reader, hasher ...hash.Hash) int
- func CopyDir(root string, fn CopyEntryFunc) error
- func CopyFile(source interface{}, destination interface{}) error
- func GetMimeType(filenameOrReader interface{}, fallback ...string) string
- func GetNthLine(file interface{}, number int) (string, error)
- func InitMime()
- func IsModifiedAfter(stat os.FileInfo, current string) bool
- func IsTerminal() bool
- func MustExpandUser(path string) string
- func MustGetNthLine(file interface{}, number int) string
- func MustReadAll(file interface{}) []byte
- func MustReadAllLines(file interface{}) []string
- func MustReadAllString(file interface{}) string
- func MustWriteFile(input interface{}, filename string) int64
- func MustWriteTempFile(input interface{}, prefix string) string
- func Open(uri string) (io.Reader, error)
- func OpenWithOptions(uri string, options OpenOptions) (io.Reader, error)
- func ReadAll(file interface{}) ([]byte, error)
- func ReadAllLines(file interface{}) ([]string, error)
- func ReadAllString(file interface{}) (string, error)
- func ReadFirstLine(file interface{}) (string, error)
- func RegisterOpenHandler(scheme string, handler OpenHandler)
- func RemoveBlankLines(data []byte) ([]byte, error)
- func RemoveOpenHandler(scheme string)
- func SameFile(first interface{}, second interface{}) bool
- func SetExt(path string, ext string, oldexts ...string) string
- func ShouldGetNthLine(file interface{}, number int) string
- func ShouldReadAllLines(file interface{}) []string
- func SizeOf(path string) convutil.Bytes
- func WriteFile(input interface{}, filename string) (int64, error)
- func WriteTempFile(input interface{}, pattern string) (string, error)
- type CopyEntryFunc
- type DirReader
- type DirReaderOption
- type DirReaderOptions
- type FileInfo
- func (self *FileInfo) IsDir() bool
- func (self *FileInfo) ModTime() time.Time
- func (self *FileInfo) Mode() os.FileMode
- func (self *FileInfo) Name() string
- func (self *FileInfo) SetIsDir(isDir bool)
- func (self *FileInfo) SetModTime(mtime time.Time)
- func (self *FileInfo) SetMode(mode os.FileMode)
- func (self *FileInfo) SetName(name string)
- func (self *FileInfo) SetSize(sz int64)
- func (self *FileInfo) SetSys(iface interface{})
- func (self *FileInfo) Size() int64
- func (self *FileInfo) Sys() interface{}
- type OpenHandler
- type OpenOptions
- type PostReadCloser
- type ReadManipulator
- type ReadManipulatorFunc
- func ManipulateAll(fns ...ReadManipulatorFunc) ReadManipulatorFunc
- func RemoveLinesContaining(needle string) ReadManipulatorFunc
- func RemoveLinesSurroundedBy(prefix string, suffix string, trimSpace bool) ReadManipulatorFunc
- func RemoveLinesWithPrefix(prefix string, trimSpace bool) ReadManipulatorFunc
- func RemoveLinesWithSuffix(suffix string, trimSpace bool) ReadManipulatorFunc
- func ReplaceWith(find string, replace string, occurrences int) ReadManipulatorFunc
- type RewriteFileSystem
- type SkipFunc
Constants ¶
const DefaultOpenTimeout = time.Duration(10 * time.Second)
Variables ¶
var CompareHasher hash.Hash = sha256.New()
var DirExists = pathutil.DirExists
Alias functions from pathutil as a convenience
var Exists = pathutil.Exists
var ExpandUser = pathutil.ExpandUser
var FileExists = pathutil.FileExists
var IsAppend = pathutil.IsAppend
var IsAppendable = pathutil.IsAppendable
var IsCharDevice = pathutil.IsCharDevice
var IsDevice = pathutil.IsDevice
var IsExclusive = pathutil.IsExclusive
var IsNamedPipe = pathutil.IsNamedPipe
var IsNonemptyDir = pathutil.IsNonemptyDir
var IsNonemptyExecutableFile = pathutil.IsNonemptyExecutableFile
var IsNonemptyFile = pathutil.IsNonemptyFile
var IsReadable = pathutil.IsReadable
var IsSetgid = pathutil.IsSetgid
var IsSetuid = pathutil.IsSetuid
var IsSocket = pathutil.IsSocket
var IsSticky = pathutil.IsSticky
var IsSymlink = pathutil.IsSymlink
var IsTemporary = pathutil.IsTemporary
var IsWritable = pathutil.IsWritable
var LinkExists = pathutil.LinkExists
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.
Functions ¶
func AddMimeTypeFile ¶
func AddMimeTypeFile(filename string)
func ChecksumFile ¶
func CompareReaders ¶
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 ¶
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 ¶
Attempt to return the nth line (starting from 1) in the given file or reader.
func IsModifiedAfter ¶
Return true if the given FileInfo sports a ModTime later than the current file.
func IsTerminal ¶
func IsTerminal() bool
func MustExpandUser ¶
func MustGetNthLine ¶
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 ¶
Same as WriteFile, but will panic if the file cannot be written.
func MustWriteTempFile ¶
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 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 ¶
Takes the given string filename, []byte, io.Reader, or io.ReadCloser and returns the bytes therein.
func ReadAllLines ¶
Read all lines of text from the given file and return them as a slice.
func ReadAllString ¶
A string version of ReadAll.
func ReadFirstLine ¶
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 ¶
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 ¶
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 ¶
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 ¶
Returns the size of the given filename, or zero if any error is encountered (or if the file is zero length).
func WriteFile ¶
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 ¶
Same as WriteFile, but writes the given input to a temporary file, returning the filename.
Types ¶
type CopyEntryFunc ¶
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) SetSkipFunc ¶
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 FailOnError )
type DirReaderOptions ¶
type DirReaderOptions []DirReaderOption
func (DirReaderOptions) Has ¶
func (self DirReaderOptions) Has(option DirReaderOption) bool
type FileInfo ¶
An os.FileInfo-compatible wrapper that allows for individual values to be overridden.
func NewFileInfo ¶
func (*FileInfo) SetModTime ¶
type OpenHandler ¶
type OpenHandler func(*url.URL, OpenOptions) (io.ReadCloser, error)
type OpenOptions ¶
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
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) Split ¶
func (self *ReadManipulator) Split(split bufio.SplitFunc)
type ReadManipulatorFunc ¶
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.