Documentation ¶
Index ¶
- func ChangeExtension(fileName string, ext string) string
- type DiskFS
- func (d DiskFS) ChangeDirectory(dir string) FS
- func (d DiskFS) Exists(filePath string) bool
- func (d DiskFS) List(dirPath string, filters ...FileFilter) ([]FileInfo, error)
- func (d DiskFS) Move(fromPath string, toPath string) error
- func (d DiskFS) Read(filePath string) (ReaderFile, error)
- func (d DiskFS) Remove(fileOrDirPath string) error
- func (d DiskFS) Stat(filePath string) (FileInfo, error)
- func (d DiskFS) WorkingDirectory() string
- func (d DiskFS) Write(filePath string) (WriterFile, error)
- type FS
- type FileFilter
- type FileInfo
- type ReaderFile
- type WriterFile
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ChangeExtension ¶
ChangeExtension helps datasets maintain the same file name stem while replacing the extension.
// Example changeExtension("foo.jpg", "txt") // "foo.txt" changeExtension("foo.bar.png", "jpg") // "foo.bar.jpg" changeExtension("foo", "txt") // "foo.txt"
Types ¶
type DiskFS ¶
type DiskFS struct {
// contains filtered or unexported fields
}
DiskFS is a file store whose operations interact w/ the local file system.
func Disk ¶
Disk creates a new file store that reads and writes files to/from the local file system. All operations will be rooted in the given directory.
Example:
files := Disk("./data") // Open ./data/input.txt for reading input, err := files.Read("input.txt") if err != nil { // handle your error nicely } defer input.Close() inputBytes, err := io.ReadAll(input)
func (DiskFS) ChangeDirectory ¶
ChangeDirectory returns a new FS that is rooted in the given subdirectory of this FS.
func (DiskFS) Exists ¶
Exists returns true when the file/directory already exits in the file system.
func (DiskFS) List ¶
func (d DiskFS) List(dirPath string, filters ...FileFilter) ([]FileInfo, error)
List performs the equivalent of the "ls" command. It returns a slice of all files and directories found in the target dirPath.
You can optionally provide a set of filters to limit which files/directories are included in the final set.
func (DiskFS) Move ¶
Move takes an existing file at the fromPath location and moves it to another spot in this file system; the toPath location.
func (DiskFS) Read ¶
func (d DiskFS) Read(filePath string) (ReaderFile, error)
Read opens the given file at the given path, providing you with an io.Reader that you can use to stream bytes from it.
func (DiskFS) Stat ¶
Stat fetches metadata about the file w/o actually opening it for reading/writing.
func (DiskFS) WorkingDirectory ¶
WorkingDirectory returns the current FS context's path/directory.
func (DiskFS) Write ¶
func (d DiskFS) Write(filePath string) (WriterFile, error)
Write opens the given file at the given path for writing. The resulting file behaves like a standard io.Writer/At.
This operation will attempt to lazy-create the parent directory(s) if it does not exist. Should the file already exist, this will overwrite its entire contents so that it only contains what you write this time.
type FS ¶
type FS interface { // WorkingDirectory returns the current FS context's path/directory. WorkingDirectory() string // Stat fetches metadata about the file w/o actually opening it for reading/writing. Stat(path string) (FileInfo, error) // Read opens the given file for reading. Read(path string) (ReaderFile, error) // Write opens the given file for writing Write(path string) (WriterFile, error) // Exists returns true when the file/directory already exits in the file system. Exists(path string) bool // List performs a UNIX style "ls" operation, giving you the names of each file // in the given directory. The filters offer a way to limit which files/dirs are included // in the final slice. // // Example: // // filesAndDirs, err := myFS.List("./conf") // jsonFiles, err := myFS.List("./conf", filestore.WithExt("json")) List(path string, filters ...FileFilter) ([]FileInfo, error) // ChangeDirectory creates a new FS in the given subdirectory. All operations on this new // instance will be rooted in the given directory. // // It should NOT matter if the directory exists or not. You still always get a valid FS // pointing to that location and only get an error when you attempt to perform some other operation. // // Example: // // usrFS := Disk("/usr") // usrLocalBinFS := usrFS.ChangeDirectory("local/bin") ChangeDirectory(path string) FS // Remove deletes the given file/directory within the file system. If the given path // is a directory, it should recursively delete it and its children. Additionally, // if you attempt to remove a file/directory that does not exist, this should behave // quietly as a nop, returning a nil error but not changing the store's state. // // Example: // // myDocumentsFS := Disk("/Users/rob/Documents") // err = myDocumentsFS.Remove("foo.txt") // if err != nil { // // could not delete file "foo.txt" // } // err = myDocumentsFS.Remove("Pictures") // if err != nil { // // could not delete directory "Pictures/" // } Remove(path string) error // Move takes an existing file at the fromPath location and moves it to another // spot in this file system; the toPath location. Move(fromPath string, toPath string) error }
FS represents a file system that you can interact with its directories and files.
type FileFilter ¶
FileFilter provides a way to exclude files/directories from a list/search.
func WithEverything ¶
func WithEverything() FileFilter
WithEverything is a dummy non-nil file filter you can use to act as though there are no filters. Basically it behaves such that all files match.
func WithExt ¶
func WithExt(extension string) FileFilter
WithExt creates a file filter that only accepts files that have a specific extension.
func WithExts ¶
func WithExts(extensions ...string) FileFilter
WithExts creates a file filter that only accepts files that have one of the given extensions.
func WithPattern ¶
func WithPattern(pattern string) FileFilter
WithPattern only allows files to pass through that match the given glob pattern.
type ReaderFile ¶
ReaderFile encapsulates a file within a file system that you can read from.
type WriterFile ¶
WriterFile encapsulates a file within a file system that you can write to.