billy

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2017 License: MIT Imports: 3 Imported by: 0

README

go-billy GoDoc Build Status codebeat badge

An interface to abstract several storages.

This library was extracted from src-d/go-git.

Installation

go get -u srcd.works/go-billy.v1/...

Why billy?

The library billy deals with storage systems and Billy is the name of a well-known, IKEA bookcase. That's it.

Usage

Billy exposes filesystems using the Filesystem interface. Each filesystem implementation gives you a New method, whose arguments depend on the implementation itself, that returns a new Filesystem.

The following example caches in memory all readable files in a directory from any billy's filesystem implementation.

func LoadToMemory(fs billy.Filesystem, path string) (*memory.Memory, error) {
	memory := memory.New()

	files, err := fs.ReadDir("/")
	if err != nil {
		return nil, err
	}

	for _, file := range files {
		if !file.IsDir() {
			orig, err := fs.Open(file.Name())
			if err != nil {
				continue
			}

			dest, err := memory.Create(file.Name())
			if err != nil {
				continue
			}

			if _, err = io.Copy(dest, orig); err != nil {
				return nil, err
			}
		}
	}

	return memory, nil
}

License

MIT, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClosed       = errors.New("file: Writing on closed file.")
	ErrReadOnly     = errors.New("this is a read-only filesystem")
	ErrNotSupported = errors.New("feature not supported")
)

Functions

This section is empty.

Types

type BaseFile

type BaseFile struct {
	BaseFilename string
	Closed       bool
}

func (*BaseFile) Filename

func (f *BaseFile) Filename() string

func (*BaseFile) IsClosed

func (f *BaseFile) IsClosed() bool

type File

type File interface {
	Filename() string
	IsClosed() bool
	io.Writer
	io.Reader
	io.Seeker
	io.Closer
}

File implements io.Closer, io.Reader, io.Seeker, and io.Writer> Provides method to obtain the file name and the state of the file (open or closed).

type FileInfo

type FileInfo os.FileInfo

type Filesystem

type Filesystem interface {
	Create(filename string) (File, error)
	Open(filename string) (File, error)
	OpenFile(filename string, flag int, perm os.FileMode) (File, error)
	Stat(filename string) (FileInfo, error)
	ReadDir(path string) ([]FileInfo, error)
	TempFile(dir, prefix string) (File, error)
	Rename(from, to string) error
	Remove(filename string) error
	Join(elem ...string) string
	Dir(path string) Filesystem
	Base() string
}

Filesystem abstract the operations in a storage-agnostic interface. It allows you to: * Create files. * Open existing files. * Get info about files. * List files in a directory. * Get a temporal file. * Rename files. * Remove files. * Join parts of path. * Obtain a filesystem starting on a subdirectory in the current filesystem. * Get the base path for the filesystem. Each method implementation varies from implementation to implementation. Refer to the specific documentation for more info.

Directories

Path Synopsis
Package memfs provides a billy filesystem base on memory.
Package memfs provides a billy filesystem base on memory.
Package os provides a billy filesystem for the OS.
Package os provides a billy filesystem for the OS.

Jump to

Keyboard shortcuts

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