filesys

package
v0.0.0-...-892de5e Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2020 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package filesys provides a standard definition of a filesys which can be implemented to abstract away the differences between a local filesystem, cloud storage, in memory data etc.

Index

Constants

This section is empty.

Variables

View Source
var ErrDirNotExist = errors.New("directory does not exist")
View Source
var ErrIsDir = errors.New("operation not valid on a directory")
View Source
var ErrIsFile = errors.New("operation not valid on a file")
View Source
var InMemNowFunc = time.Now

InMemNowFunc is a func() time.Time that can be used to supply the current time. The default value gets the current time from the system clock, but it can be set to something else in order to support reproducible tests.

View Source
var LocalFS = &localFS{}

LocalFS is the machines local filesystem

Functions

func UnmarshalJSONFile

func UnmarshalJSONFile(fs ReadableFS, path string, dest interface{}) error

Types

type FSIterCB

type FSIterCB func(path string, size int64, isDir bool) (stop bool)

FSIterCB specifies the signature of the function that will be called for every item found while iterating.

type Filesys

type Filesys interface {
	ReadableFS
	WritableFS
	WalkableFS
}

Filesys is an interface whose implementors will provide read, write, and list mechanisms

func LocalFilesysWithWorkingDir

func LocalFilesysWithWorkingDir(cwd string) (Filesys, error)

LocalFilesysWithWorkingDir returns a new Filesys implementation backed by the local filesystem with the supplied working directory. Path relative operations occur relative to this directory.

type FilesysLock

type FilesysLock interface {
	TryLock() (bool, error)
	Unlock() error
}

FilesysLock is an interface for locking and unlocking filesystems

func CreateFilesysLock

func CreateFilesysLock(fs Filesys, filename string) FilesysLock

CreateFilesysLock creates a new FilesysLock

type InMemFS

type InMemFS struct {
	// contains filtered or unexported fields
}

InMemFS is an in memory filesystem implementation that is primarily intended for testing

func EmptyInMemFS

func EmptyInMemFS(workingDir string) *InMemFS

EmptyInMemFS creates an empty InMemFS instance

func NewInMemFS

func NewInMemFS(dirs []string, files map[string][]byte, cwd string) *InMemFS

NewInMemFS creates an InMemFS with directories and folders provided.

func (*InMemFS) Abs

func (fs *InMemFS) Abs(path string) (string, error)

converts a path to an absolute path. If it's already an absolute path the input path will be returned unaltered

func (*InMemFS) Delete

func (fs *InMemFS) Delete(path string, force bool) error

Delete will delete an empty directory, or a file. If trying delete a directory that is not empty you can set force to true in order to delete the dir and all of it's contents

func (*InMemFS) DeleteFile

func (fs *InMemFS) DeleteFile(path string) error

DeleteFile will delete a file at the given path

func (*InMemFS) Exists

func (fs *InMemFS) Exists(path string) (exists bool, isDir bool)

Exists will tell you if a file or directory with a given path already exists, and if it does is it a directory

func (*InMemFS) Iter

func (fs *InMemFS) Iter(path string, recursive bool, cb FSIterCB) error

Iter iterates over the files and subdirectories within a given directory (Optionally recursively). There are no guarantees about the ordering of results. It is also possible that concurrent delete operations could render a file path invalid when the callback is made.

func (*InMemFS) LastModified

func (fs *InMemFS) LastModified(path string) (t time.Time, exists bool)

LastModified gets the last modified timestamp for a file or directory at a given path

func (*InMemFS) MkDirs

func (fs *InMemFS) MkDirs(path string) error

MkDirs creates a folder and all the parent folders that are necessary to create it.

func (*InMemFS) MoveFile

func (fs *InMemFS) MoveFile(srcPath, destPath string) error

MoveFile will move a file from the srcPath in the filesystem to the destPath

func (*InMemFS) OpenForRead

func (fs *InMemFS) OpenForRead(fp string) (io.ReadCloser, error)

OpenForRead opens a file for reading

func (*InMemFS) OpenForWrite

func (fs *InMemFS) OpenForWrite(fp string, perm os.FileMode) (io.WriteCloser, error)

OpenForWrite opens a file for writing. The file will be created if it does not exist, and if it does exist it will be overwritten.

func (*InMemFS) ReadFile

func (fs *InMemFS) ReadFile(fp string) ([]byte, error)

ReadFile reads the entire contents of a file

func (*InMemFS) WriteFile

func (fs *InMemFS) WriteFile(fp string, data []byte) error

WriteFile writes the entire data buffer to a given file. The file will be created if it does not exist, and if it does exist it will be overwritten.

type InMemFileLock

type InMemFileLock struct {
	// contains filtered or unexported fields
}

InMemFileLock is a lock for the InMemFS

func NewInMemFileLock

func NewInMemFileLock(fs Filesys) *InMemFileLock

NewInMemFileLock creates a new InMemFileLock

func (*InMemFileLock) TryLock

func (memLock *InMemFileLock) TryLock() (bool, error)

TryLock attempts to lock the lock or fails if it is already locked

func (*InMemFileLock) Unlock

func (memLock *InMemFileLock) Unlock() error

Unlock unlocks the lock

type LocalFileLock

type LocalFileLock struct {
	// contains filtered or unexported fields
}

LocalFileLock is the lock for the localFS

func NewLocalFileLock

func NewLocalFileLock(fs Filesys, filename string) *LocalFileLock

NewLocalFileLock creates a new LocalFileLock

func (*LocalFileLock) TryLock

func (locLock *LocalFileLock) TryLock() (bool, error)

TryLock attempts to lock the lock or fails if it is already locked

func (*LocalFileLock) Unlock

func (locLock *LocalFileLock) Unlock() error

Unlock unlocks the lock

type ReadWriteFS

type ReadWriteFS interface {
	ReadableFS
	WritableFS
}

ReadWriteFS is an interface whose implementors will provide read, and write implementations but may not allow for files to be listed.

type ReadableFS

type ReadableFS interface {

	// OpenForRead opens a file for reading
	OpenForRead(fp string) (io.ReadCloser, error)

	// ReadFile reads the entire contents of a file
	ReadFile(fp string) ([]byte, error)

	// Exists will tell you if a file or directory with a given path already exists, and if it does is it a directory
	Exists(path string) (exists bool, isDir bool)

	// converts a path to an absolute path.  If it's already an absolute path the input path will be returned unaltered
	Abs(path string) (string, error)

	// LastModified gets the last modified timestamp for a file or directory at a given path
	LastModified(path string) (t time.Time, exists bool)
}

ReadableFS is an interface providing read access to objs in a filesystem

type WalkableFS

type WalkableFS interface {

	// Iter iterates over the files and subdirectories within a given directory (Optionally recursively).  There
	// are no guarantees about the ordering of results.  Two calls to iterate the same directory may yield
	// differently ordered results.
	Iter(directory string, recursive bool, cb FSIterCB) error
}

WalkableFS is an interface for walking the files and subdirectories of a directory

type WritableFS

type WritableFS interface {
	// OpenForWrite opens a file for writing.  The file will be created if it does not exist, and if it does exist
	// it will be overwritten.
	OpenForWrite(fp string, perm os.FileMode) (io.WriteCloser, error)

	// WriteFile writes the entire data buffer to a given file.  The file will be created if it does not exist,
	// and if it does exist it will be overwritten.
	WriteFile(fp string, data []byte) error

	// MkDirs creates a folder and all the parent folders that are necessary to create it.
	MkDirs(path string) error

	// DeleteFile will delete a file at the given path
	DeleteFile(path string) error

	// Delete will delete an empty directory, or a file.  If trying delete a directory that is not empty you can set force to
	// true in order to delete the dir and all of it's contents
	Delete(path string, force bool) error

	// MoveFile will move a file from the srcPath in the filesystem to the destPath
	MoveFile(srcPath, destPath string) error
}

WritableFS is an interface providing write access to objs in a filesystem

Jump to

Keyboard shortcuts

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