src-d / go-billy

The missing interface filesystem abstraction for Go
https://godoc.org/gopkg.in/src-d/go-billy.v4
Apache License 2.0
199 stars 43 forks source link
abstraction filesystem golang

WE CONTINUE THE DEVELOPMENT AT go-git/go-billy. This repository is abandoned, and no further updates will be done on the code base, nor issue/prs will be answered or attended.

go-billy GoDoc Build Status Build status codecov

The missing interface filesystem abstraction for Go. Billy implements an interface based on the os standard library, allowing to develop applications without dependency on the underlying storage. Makes it virtually free to implement mocks and testing over filesystem operations.

Billy was born as part of src-d/go-git project.

Installation

go get -u gopkg.in/src-d/go-billy.v4/...

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(origin billy.Filesystem, path string) (*memory.Memory, error) {
    memory := memory.New()

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

    for _, file := range files {
        if file.IsDir() {
            continue
        }

        src, err := origin.Open(file.Name())
        if err != nil {
            return nil, err
        }

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

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

        if err := dst.Close(); err != nil {
            return nil, err
        }

        if err := src.Close(); err != nil {
            return nil, err
        }
    }

    return memory, nil
}

Why billy?

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

License

Apache License Version 2.0, see LICENSE