filesystem

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WatchOpCreate = fsnotify.Create
	WatchOpWrite  = fsnotify.Write
	WatchOpRemove = fsnotify.Remove
	WatchOpRename = fsnotify.Rename
	WatchOpChmod  = fsnotify.Chmod
)
View Source
const (
	DefaultMaxFileSize = 3 * 1024 * 1024 // 3MByte
)

Variables

View Source
var (
	ErrNonExistentWatch = fsnotify.ErrNonExistentWatch
	ErrEventOverflow    = fsnotify.ErrEventOverflow
)
View Source
var (
	// Mobile is a FileSystem for the mobile environment that
	// requires absolute path to access file since the notion of
	// current directory is different from desktop.
	Mobile = &AbsPathFileSystem{
		CurrentDir: "",
		Backend:    Desktop,
	}
)

Functions

func Exist

func Exist(filepath string) bool

func Glob

func Glob(pattern string) ([]string, error)

Glob is wrap function for filepath.Glob with use filesystem.Default

func GlobFS

func GlobFS(fs FileSystemPR, pattern string) ([]string, error)

Glob is wrap function for filepath.Glob with use filesystem.FileSystemPR

func Load

func Load(filepath string) (reader io.ReadCloser, err error)

func ResolvePath

func ResolvePath(path string) (string, error)

ResolvePath resolve file path under filesystem.Default. if Default also implements PathResolver, use it to resolve path, otherwise returns path itself.

func ResolvePathFS

func ResolvePathFS(fs FileSystemPR, path string) (string, error)

ResolvePathFS resolve file path under given FileSystem. if FileSystem also implements PathResolver, use it to resolve path, otherwise returns path itself.

func Store

func Store(filepath string) (io.WriteCloser, error)

func StringReadCloser

func StringReadCloser(content string) (io.ReadCloser, error)

StringReadCloser is helper function which creates io.ReadCloser from a entire content to adapt Loader interface.

Types

type AbsPathFileSystem

type AbsPathFileSystem struct {
	CurrentDir string
	Backend    FileSystem
}

AbsPathFileSystem completes absolute path for every file access. The absolute path is made by using filepath.Abs when CurrentDir is set to empty, or made by file.Join(CurrentDir, relativePath) when CurrentDir is set. The Backend is used to access File API. and The OSFileSystem is used as Backend when it is nil.

func (*AbsPathFileSystem) Exist

func (absfs *AbsPathFileSystem) Exist(fpath string) bool

func (*AbsPathFileSystem) Load

func (absfs *AbsPathFileSystem) Load(fpath string) (reader io.ReadCloser, err error)

func (*AbsPathFileSystem) ResolvePath

func (absfs *AbsPathFileSystem) ResolvePath(fpath string) (string, error)

ResolvePath complete parent directory path to fpath when fpath is a relative path. It returns fpath itself when fpath is already absolute path.

func (*AbsPathFileSystem) Store

func (absfs *AbsPathFileSystem) Store(fpath string) (writer io.WriteCloser, err error)

type FileSystem

type FileSystem interface {
	Loader

	// create data store entry.
	Store(filepath string) (io.WriteCloser, error)
}

abstraction for the filesystem.

type FileSystemPR added in v0.7.0

type FileSystemPR interface {
	FileSystem
	PathResolver
}

FileSystemPR is a composit interface with FileSystem and PathResolver interfaces.

var (
	// Default is a default FileSystem to be used by exported functions.
	Default FileSystemPR = Desktop
)

type Loader

type Loader interface {
	// Load loads content specified by the relative path
	// from erago system root directory.
	// It returns io.Reader for the loaded content with no error,
	// or returns nil with file loading error.
	Load(filepath string) (reader io.ReadCloser, err error)

	// Exist checks whether given filepath exist from erago system root directory.
	// It returns true when the filepath exists, otherwise return false.
	Exist(filepath string) bool
}

Loader is a platform depended file loader which searches file path and return its content as io.Reader.

var (
	// Desktop is a FileSystem for the desktop environment
	Desktop = &OSFileSystem{MaxFileSize: DefaultMaxFileSize}
	// String is a adaptation of strings.Buffer with Loader interface.
	String Loader = LoaderFunc(StringReadCloser)
)

type LoaderFunc

type LoaderFunc func(string) (io.ReadCloser, error)

LoaderFunc implements Loader interface.

func (LoaderFunc) Exist

func (fn LoaderFunc) Exist(filepath string) bool

func (LoaderFunc) Load

func (fn LoaderFunc) Load(filepath string) (reader io.ReadCloser, err error)

type NopPathResolver added in v0.7.0

type NopPathResolver struct{}

NopPathResolver implements PathResolver interface.

func (NopPathResolver) ResolvePath added in v0.7.0

func (NopPathResolver) ResolvePath(path string) (string, error)

ResolvePath returns path as is and no error.

type OSFileSystem

type OSFileSystem struct {
	MaxFileSize int64 // in bytes
}

OSFileSystem is a adaptation of the os.Open() with Loader interface.

func (*OSFileSystem) Exist

func (osfs *OSFileSystem) Exist(filepath string) bool

func (*OSFileSystem) Load

func (osfs *OSFileSystem) Load(filepath string) (reader io.ReadCloser, err error)

func (*OSFileSystem) ResolvePath added in v0.7.0

func (osfs *OSFileSystem) ResolvePath(fpath string) (string, error)

func (*OSFileSystem) Store

func (osfs *OSFileSystem) Store(fpath string) (writer io.WriteCloser, err error)

type PathResolver

type PathResolver interface {
	ResolvePath(path string) (string, error)
}

path resolver resolves file path on the filesystem.

type RFileSystemPR added in v0.7.0

type RFileSystemPR interface {
	Loader
	PathResolver
}

RFileSystemPR is a Readonly FileSystemPR interface.

type WatchEvent added in v0.7.0

type WatchEvent = fsnotify.Event

Export some types and values so that user need not import underlying package explicitly

type WatchOp added in v0.7.0

type WatchOp = fsnotify.Op

type Watcher added in v0.7.0

type Watcher interface {
	Watch(filepath string) error
	UnWatch(filepath string) error
	Events() <-chan WatchEvent
	Errors() <-chan error
	Close() error
}

func OpenWatcher added in v0.7.0

func OpenWatcher() (Watcher, error)

OpenWatcher creates Watcher interface from Default FileSystem. Note that returned watcher must call Close() after use.

func OpenWatcherPR added in v0.7.0

func OpenWatcherPR(pr PathResolver) (Watcher, error)

OpenWatcherPR creates Watcher interface from given PathResolver.

Jump to

Keyboard shortcuts

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