fs

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2019 License: MIT Imports: 8 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// DefaultLineDelimiter denotes the character or characther sequence to separate lines in text files.
	DefaultLineDelimiter = "\n"
)

Variables

View Source
var (
	// Err is a generic file system related error.
	Err = errors.New("A file system error occured")
	// ErrNotSupported is returned when using a function that is not supported.
	ErrNotSupported = errors.New("Operation %s is not supported by the file system")
	// ErrNotExists occurs when an action failed becaus of a missing element of unknown type(file or directory).
	ErrNotExists = errors.New("The path %q does not exist")
	// ErrFileNotExists occurs when an action failed because of a missing file.
	ErrFileNotExists = errors.New("The file %q does not exist")
	// ErrDirectoryNotExists occurs when an action failed because of a missing directory.
	ErrDirectoryNotExists = errors.New("The directory %q does not exist")
	// ErrAccessDenied denotes an error caused by insufficient privileges.
	ErrAccessDenied = errors.New("Access to %q denied")
	// ErrNotEmpty occurs when trying to delete a non-empty directory without recursive flag.
	ErrNotEmpty = errors.New("The directory is not empty")
)
View Source
var (
	// OrderDefault sorts elements lexicographically ascending and moves directories to the top of the list.
	OrderDefault = func(f1, f2 FileInfo) int {
		if order := OrderDirectoriesFirst(f1, f2); order != 0 {
			return order
		}
		return OrderLexicographicAsc(f1, f2)
	}

	// OrderFilesFirst moves files to the top of the list.
	OrderFilesFirst = func(f1, f2 FileInfo) int {
		if !f1.IsDir() && f2.IsDir() {
			return -1
		} else if f1.IsDir() && !f2.IsDir() {
			return 1
		}
		return 0
	}

	// OrderDirectoriesFirst moves directories to the top of the list.
	OrderDirectoriesFirst = func(f1, f2 FileInfo) int {
		return -OrderFilesFirst(f1, f2)
	}

	// OrderLexicographicAsc moves elements starting with A to the top of the list.
	OrderLexicographicAsc = func(f1, f2 FileInfo) int {
		if f1.Name() < f2.Name() {
			return -1
		} else if f1.Name() > f2.Name() {
			return 1
		}
		return 0
	}

	// OrderLexicographicDesc moves elements starting with Z to the top of the list.
	OrderLexicographicDesc = func(f1, f2 FileInfo) int {
		return -OrderLexicographicAsc(f1, f2)
	}
)

Functions

func CleanDir added in v0.1.0

func CleanDir(path string) errors.Error

CleanDir removes all files and directories from a directory.

func Copy

func Copy(src, dst string) errors.Error

Copy clone a file or directory to the target. If the target already exists, it must be the same element type (file or directory) to be overwritten.

func CopyAll added in v0.1.1

func CopyAll(src, dst string) errors.Error

CopyAll copies all files and directories contained in src to dst.

func CopyDir

func CopyDir(src, dst string) errors.Error

CopyDir recursively clones a directory overwriting all existing files.

func CopyFile

func CopyFile(src, dst string) errors.Error

CopyFile clones a file and overwrites the existing one.

func CreateDirectory added in v0.1.1

func CreateDirectory(path string) errors.Error

CreateDirectory creates a new directory and all parent directories if they do not exist.

func DeleteDirectory added in v0.1.0

func DeleteDirectory(path string, recursive bool) errors.Error

DeleteDirectory deletes an empty directory. If recursive is set, all contained items will be deleted first.

func DeleteFile added in v0.1.0

func DeleteFile(path string) errors.Error

DeleteFile deletes a file.

func Exists

func Exists(path string) (bool, errors.Error)

Exists returns true, if the given path is a file or directory.

func GetTempDir added in v0.1.1

func GetTempDir(prefix string) (string, errors.Error)

GetTempDir returns the path to an empty temporary dir.

func GetTempFile added in v0.1.1

func GetTempFile(pattern string) (string, errors.Error)

GetTempFile returns the path to an empty temporary file.

func IsDir

func IsDir(path string) (bool, errors.Error)

IsDir returns true, if the given path is a directory.

func IsFile

func IsFile(path string) (bool, errors.Error)

IsFile returns true, if the given path is a file.

func Move added in v0.2.0

func Move(src, dst string) errors.Error

Move moves a file or directory to a new location. If the target already exists, it must be the same element type (file or directory) to be overwritten.

func MoveAll added in v0.1.1

func MoveAll(src, dst string) errors.Error

MoveAll moves all files and directories contained in src to dst.

func MoveDir added in v0.1.0

func MoveDir(src, dst string) errors.Error

MoveDir moves a directory to a new location.

func MoveFile added in v0.1.0

func MoveFile(src, dst string) errors.Error

MoveFile moves a file to a new location.

func ReadBytes added in v0.1.0

func ReadBytes(path string) ([]byte, errors.Error)

ReadBytes returns all bytes contained in a file.

func ReadLines added in v0.0.2

func ReadLines(path string) ([]string, errors.Error)

ReadLines returns all lines separated by "\n", "\r" or "\r\n" from a file.

func ReadString added in v0.1.0

func ReadString(path string) (string, errors.Error)

ReadString returns the file content as string.

func Sort added in v0.2.1

func Sort(files []FileInfo, cmp FileInfoComparer)

Sort sorts an array of FileInfo objects using the given comparer.

func Walk added in v0.2.1

func Walk(dir string, visitFileHandler VisitFileHandler, enterDirHandler EnterDirHandler, leaveDirHandler LeaveDirHandler, options *WalkOptions) errors.Error

Walk calls the corresponding callback functions for ever file and directory contained in dir recursively.

The visit handler is called first for every file and directory that is found inside a directory. For directories, the enter dir handler is called subsequently. After this call, Walk instantly recurses into the given directory. Remaining files in the parent directory are visited after the corresponding leave callback. Leave callbacks are performed directly after the last element of a directory has been visited (and leaved in case of a sub-directory).

func WithTempDir

func WithTempDir(prefix string, f func(tmpDir string) errors.Error) errors.Error

WithTempDir creates a temporary directory and deletes it when f returns.

func WithTempFile

func WithTempFile(pattern string, f func(tmpFile string) errors.Error) errors.Error

WithTempFile creates a temporary file and deletes it when f returns.

func WriteBytes added in v0.1.0

func WriteBytes(path string, content []byte) errors.Error

WriteBytes writes all bytes to a file.

func WriteLines added in v0.0.3

func WriteLines(path string, lines []string) errors.Error

WriteLines writes all lines separated by the default line separator to a file.

func WriteString added in v0.1.0

func WriteString(path, content string) errors.Error

WriteString writes a string to a file.

Types

type EnterDirHandler added in v0.2.0

type EnterDirHandler func(dir string, f FileInfo, isRoot bool, skipDir *bool) errors.Error // bool isRootDir

EnterDirHandler is called by Walk before a directory is entered. If skipDir is set to true, the directory will not be visited.

type File added in v0.0.2

type File interface {
	io.Reader
	io.Writer
	io.Closer
	io.Seeker
}

File is the instance object for an opened file.

func CreateFile added in v0.1.0

func CreateFile(path string) (File, errors.Error)

CreateFile a new file (or truncate an existing) and return the file instance handle.

func Open added in v0.1.0

func Open(path string) (File, errors.Error)

Open opens a file instance for reading and returns the handle.

func OpenFile added in v0.1.0

func OpenFile(path string, flags OpenFlags) (File, errors.Error)

OpenFile opens a general purpose file instance based on flags and returns the handle.

type FileInfo added in v0.0.2

type FileInfo interface {
	Name() string
	Size() int64
	IsDir() bool
}

FileInfo contains meta information for a file.

func ReadDir added in v0.1.0

func ReadDir(path string) ([]FileInfo, errors.Error)

ReadDir returns all files and directories contained in a directory.

func Stat added in v0.2.1

func Stat(path string) (FileInfo, errors.Error)

Stat returns file or directory stats for a given path.

type FileInfoComparer added in v0.2.1

type FileInfoComparer func(f1, f2 FileInfo) int

FileInfoComparer returns true, when f1 should be displayed before f2.

func NewCompoundComparer added in v0.2.1

func NewCompoundComparer(compareFuncs ...FileInfoComparer) FileInfoComparer

NewCompoundComparer returns a new comparer based on the prioritized list of compare functions. The first comparer has the highest priority.

type FileSystem

type FileSystem struct {
	LineSeparator string
	// contains filtered or unexported fields
}

FileSystem offers advanced functionality based on a file system driver.

var (
	// DefaultFileSystem denots the file system used for all default accessors.
	DefaultFileSystem *FileSystem
)

func New added in v0.0.2

func New() *FileSystem

New returns a new file system with local file system driver.

func NewWithDriver added in v0.1.0

func NewWithDriver(driver interface{}) *FileSystem

NewWithDriver returns a new file system using the given file system driver. The given driver must implement at least one of the file system driver interfaces.

func (*FileSystem) CanAll added in v0.1.0

func (fs *FileSystem) CanAll() bool

CanAll returns true when the file system offers complete functionality.

func (*FileSystem) CanNavigate added in v0.2.0

func (fs *FileSystem) CanNavigate() bool

CanNavigate returns true when the file system allows to list files and directories.

func (*FileSystem) CanRead added in v0.1.0

func (fs *FileSystem) CanRead() bool

CanRead returns true when the file system can perform read operations.

func (*FileSystem) CanReadWrite added in v0.1.0

func (fs *FileSystem) CanReadWrite() bool

CanReadWrite returns true when the file system can perform both read and write operations.

func (*FileSystem) CanTemp added in v0.1.0

func (fs *FileSystem) CanTemp() bool

CanTemp returns true when the file system can create temporary files and directories.

func (*FileSystem) CanWrite added in v0.1.0

func (fs *FileSystem) CanWrite() bool

CanWrite returns true when the file system can perform write operations.

func (*FileSystem) CleanDir added in v0.1.0

func (fs *FileSystem) CleanDir(dir string) errors.Error

CleanDir removes all files and directories from a directory.

func (*FileSystem) Copy added in v0.1.1

func (fs *FileSystem) Copy(src, dst string) errors.Error

Copy clone a file or directory to the target. If the target already exists, it must be the same element type (file or directory) to be overwritten.

func (*FileSystem) CopyAll added in v0.1.1

func (fs *FileSystem) CopyAll(src, dst string) errors.Error

CopyAll copies all files and directories contained in src to dst.

func (*FileSystem) CopyDir added in v0.1.1

func (fs *FileSystem) CopyDir(src, dst string) errors.Error

CopyDir recursively clones a directory overwriting all existing files.

func (*FileSystem) CopyFile added in v0.1.0

func (fs *FileSystem) CopyFile(src, dst string) errors.Error

CopyFile clones a file and overwrites the existing one.

func (*FileSystem) CreateDirectory added in v0.1.1

func (fs *FileSystem) CreateDirectory(path string) errors.Error

CreateDirectory creates a new directory and all parent directories if they do not exist.

func (*FileSystem) CreateFile added in v0.1.0

func (fs *FileSystem) CreateFile(path string) (File, errors.Error)

CreateFile a new file (or truncate an existing) and return the file instance handle.

func (*FileSystem) DeleteDirectory added in v0.1.0

func (fs *FileSystem) DeleteDirectory(path string, recursive bool) errors.Error

DeleteDirectory deletes an empty directory. If recursive is set, all contained items will be deleted first.

func (*FileSystem) DeleteFile added in v0.1.0

func (fs *FileSystem) DeleteFile(path string) errors.Error

DeleteFile deletes a file.

func (*FileSystem) Exists added in v0.1.0

func (fs *FileSystem) Exists(path string) (bool, errors.Error)

Exists returns true, if the given path is a file or directory.

func (*FileSystem) GetTempDir added in v0.1.1

func (fs *FileSystem) GetTempDir(prefix string) (string, errors.Error)

GetTempDir returns the path to an empty temporary dir.

func (*FileSystem) GetTempFile added in v0.1.1

func (fs *FileSystem) GetTempFile(pattern string) (string, errors.Error)

GetTempFile returns the path to an empty temporary file.

func (*FileSystem) IsDir added in v0.1.0

func (fs *FileSystem) IsDir(path string) (bool, errors.Error)

IsDir returns true, if the given path is a directory.

func (*FileSystem) IsFile added in v0.1.0

func (fs *FileSystem) IsFile(path string) (bool, errors.Error)

IsFile returns true, if the given path is a file.

func (*FileSystem) Move added in v0.2.0

func (fs *FileSystem) Move(src, dst string) errors.Error

Move moves a file or directory to a new location. If the target already exists, it must be the same element type (file or directory) to be overwritten.

func (*FileSystem) MoveAll added in v0.1.1

func (fs *FileSystem) MoveAll(src, dst string) errors.Error

MoveAll moves all files and directories contained in src to dst.

func (*FileSystem) MoveDir added in v0.1.0

func (fs *FileSystem) MoveDir(src, dst string) errors.Error

MoveDir moves a directory to a new location.

func (*FileSystem) MoveFile added in v0.1.0

func (fs *FileSystem) MoveFile(src, dst string) errors.Error

MoveFile moves a file to a new location.

func (*FileSystem) Open added in v0.1.0

func (fs *FileSystem) Open(path string) (File, errors.Error)

Open opens a file instance for reading and returns the handle.

func (*FileSystem) OpenFile added in v0.1.0

func (fs *FileSystem) OpenFile(path string, flags OpenFlags) (File, errors.Error)

OpenFile opens a general purpose file instance based on flags and returns the handle.

func (*FileSystem) ReadBytes added in v0.1.0

func (fs *FileSystem) ReadBytes(path string) ([]byte, errors.Error)

ReadBytes returns all bytes contained in a file.

func (*FileSystem) ReadDir added in v0.1.0

func (fs *FileSystem) ReadDir(path string) ([]FileInfo, errors.Error)

ReadDir returns all files and directories contained in a directory.

func (*FileSystem) ReadLines added in v0.1.0

func (fs *FileSystem) ReadLines(path string) ([]string, errors.Error)

ReadLines returns all files contained in a text file.

func (*FileSystem) ReadString added in v0.1.0

func (fs *FileSystem) ReadString(path string) (string, errors.Error)

ReadString returns the file content as string.

func (*FileSystem) Stat added in v0.2.1

func (fs *FileSystem) Stat(path string) (FileInfo, errors.Error)

Stat returns file or directory stats for a given path.

func (*FileSystem) Walk added in v0.2.0

func (fs *FileSystem) Walk(dir string, visitFileHandler VisitFileHandler, enterDirHandler EnterDirHandler, leaveDirHandler LeaveDirHandler, options *WalkOptions) errors.Error

Walk calls the corresponding callback functions for ever file and directory contained in dir recursively.

The visit handler is called first for every file and directory that is found inside a directory. For directories, the enter dir handler is called subsequently. After this call, Walk instantly recurses into the given directory. Remaining files in the parent directory are visited after the corresponding leave callback. Leave callbacks are performed directly after the last element of a directory has been visited (and leaved in case of a sub-directory).

func (*FileSystem) WithTempDir added in v0.1.0

func (fs *FileSystem) WithTempDir(prefix string, f func(tmpDir string) errors.Error) errors.Error

WithTempDir creates a temporary directory and deletes it when f returns.

func (*FileSystem) WithTempFile added in v0.1.0

func (fs *FileSystem) WithTempFile(pattern string, f func(tmpFile string) errors.Error) errors.Error

WithTempFile creates a temporary file and deletes it when f returns.

func (*FileSystem) WriteBytes added in v0.1.0

func (fs *FileSystem) WriteBytes(path string, content []byte) errors.Error

WriteBytes writes all bytes to a file.

func (*FileSystem) WriteLines added in v0.1.0

func (fs *FileSystem) WriteLines(path string, lines []string) errors.Error

WriteLines writes all lines to a file using the default line delimiter.

func (*FileSystem) WriteString added in v0.1.0

func (fs *FileSystem) WriteString(path, content string) errors.Error

WriteString writes a string to a file.

type FileSystemDriver added in v0.0.2

type FileSystemDriver interface {
	TempFileSystemDriver
}

FileSystemDriver describes a complete file system function set.

type LeaveDirHandler added in v0.2.0

type LeaveDirHandler func(dir string, f FileInfo, isRoot bool) errors.Error

LeaveDirHandler is called by Walk after all elements inside a directory have been processed.

type LocalDriver added in v0.0.2

type LocalDriver struct {
	Root string
}

LocalDriver allows access to the file system of the host machine.

func (*LocalDriver) CreateDirectory added in v0.1.0

func (d *LocalDriver) CreateDirectory(path string) errors.Error

CreateDirectory creates a new directory and all parent directories if they do not exist.

func (*LocalDriver) DeleteDirectory added in v0.1.0

func (d *LocalDriver) DeleteDirectory(path string, recursive bool) errors.Error

DeleteDirectory deletes an empty directory. Set recursive to true to also remove directory content.

func (*LocalDriver) DeleteFile added in v0.1.0

func (d *LocalDriver) DeleteFile(path string) errors.Error

DeleteFile deletes a file.

func (*LocalDriver) Exists added in v0.0.2

func (d *LocalDriver) Exists(path string) (bool, errors.Error)

Exists returns true, if the given path is a file or directory.

func (*LocalDriver) GetTempDir added in v0.1.0

func (d *LocalDriver) GetTempDir(prefix string) (string, errors.Error)

GetTempDir returns the path to an empty temporary dir.

func (*LocalDriver) GetTempFile added in v0.0.2

func (d *LocalDriver) GetTempFile(pattern string) (string, errors.Error)

GetTempFile returns the path to an empty temporary file.

func (*LocalDriver) IsDir added in v0.0.2

func (d *LocalDriver) IsDir(path string) (bool, errors.Error)

IsDir returns true, if the given path is a directory.

func (*LocalDriver) IsFile added in v0.0.2

func (d *LocalDriver) IsFile(path string) (bool, errors.Error)

IsFile returns true, if the given path is a file.

func (*LocalDriver) MoveDir added in v0.1.0

func (d *LocalDriver) MoveDir(src, dst string) errors.Error

MoveDir moves a directory to a new location.

func (*LocalDriver) MoveFile added in v0.1.0

func (d *LocalDriver) MoveFile(src, dst string) errors.Error

MoveFile moves a file to a new location.

func (*LocalDriver) OpenFile added in v0.1.0

func (d *LocalDriver) OpenFile(path string, flags OpenFlags) (File, errors.Error)

OpenFile opens a file instance and returns the handle.

func (*LocalDriver) ReadDir added in v0.1.0

func (d *LocalDriver) ReadDir(path string) ([]FileInfo, errors.Error)

ReadDir returns all files and directories contained in a directory.

func (*LocalDriver) Stat added in v0.2.1

func (d *LocalDriver) Stat(path string) (FileInfo, errors.Error)

Stat returns file or directory stats for a given path.

type NavigationFileSystemDriver interface {
	Exists(path string) (bool, errors.Error)
	IsFile(path string) (bool, errors.Error)
	IsDir(path string) (bool, errors.Error)

	Stat(path string) (FileInfo, errors.Error)
	ReadDir(path string) ([]FileInfo, errors.Error)
}

NavigationFileSystemDriver describes functionality to list files and directories but does not allow access to file content.

type OpenFlags added in v0.1.0

type OpenFlags int

OpenFlags specifies information on how to open a file.

const (
	// OpenReadOnly denotes opening a file using read-only access.
	OpenReadOnly OpenFlags = OpenFlags(os.O_RDONLY)
	// OpenWriteOnly denotes opening a file using write-only access.
	OpenWriteOnly OpenFlags = OpenFlags(os.O_WRONLY)
	// OpenReadWrite denotes opening a file using read-write access.
	OpenReadWrite OpenFlags = OpenFlags(os.O_RDWR)
)

func (OpenFlags) Access added in v0.1.1

func (flag OpenFlags) Access() OpenFlags

Access returns only the access flag.

func (OpenFlags) Append added in v0.1.0

func (flag OpenFlags) Append() OpenFlags

Append opens the file for appending.

func (OpenFlags) Create added in v0.1.0

func (flag OpenFlags) Create() OpenFlags

Create creates the file if it does not exist.

func (OpenFlags) Exclusive added in v0.1.0

func (flag OpenFlags) Exclusive() OpenFlags

Exclusive opens the file for appending.

func (OpenFlags) IsRead added in v0.1.0

func (flag OpenFlags) IsRead() bool

IsRead returns whether the given flags require read access.

func (OpenFlags) IsWrite added in v0.1.0

func (flag OpenFlags) IsWrite() bool

IsWrite returns whether the given flags require write access.

func (OpenFlags) Sync added in v0.1.0

func (flag OpenFlags) Sync() OpenFlags

Sync opens the file for appending.

func (OpenFlags) Truncate added in v0.1.0

func (flag OpenFlags) Truncate() OpenFlags

Truncate opens the file for appending.

type ReadFileSystemDriver added in v0.0.2

type ReadFileSystemDriver interface {
	NavigationFileSystemDriver

	OpenFile(path string, flags OpenFlags) (File, errors.Error)
}

ReadFileSystemDriver describes functionality to read from a file system.

type ReadWriteFileSystemDriver added in v0.0.2

type ReadWriteFileSystemDriver interface {
	ReadFileSystemDriver

	CreateDirectory(path string) errors.Error

	DeleteFile(path string) errors.Error
	DeleteDirectory(path string, recursive bool) errors.Error

	MoveFile(src, dst string) errors.Error
	MoveDir(src, dst string) errors.Error
}

ReadWriteFileSystemDriver describes functionality to write to a file system.

type TempFileSystemDriver added in v0.0.2

type TempFileSystemDriver interface {
	ReadWriteFileSystemDriver

	GetTempFile(pattern string) (string, errors.Error)
	GetTempDir(prefix string) (string, errors.Error)
}

TempFileSystemDriver describes functionality to create temporary files and directories on a file system.

type VisitFileHandler added in v0.2.0

type VisitFileHandler func(dir string, f FileInfo, isRoot bool) errors.Error

VisitFileHandler is called by Walk for every file and directory that is found recursively.

type WalkOptions added in v0.2.0

type WalkOptions struct {
	// SkipSubDirs denotes whether the directory is traversed recursively or not.
	SkipSubDirs bool
	// VisitRootDir causes Walk to also call the visit handler for the walked directory itself.
	VisitRootDir bool
	// EnterLeaveCallbacksForRoot denotes whether the enter and leave callbacks are called for the walked directory itself.
	EnterLeaveCallbacksForRoot bool
	// VisitOrder denotes a function that is used to sort the sequence of files inside a single directory to specify in which order the sub-elements are processed.
	VisitOrder FileInfoComparer
}

WalkOptions can be used to specify the behavior of Walk like visit order and search strategy.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL