fs/ricefs/fs.go

105 lines
2.9 KiB
Go

package ricefs
import (
"errors"
"os"
"time"
rice "github.com/GeertJohan/go.rice"
"github.com/spf13/afero"
)
var _ afero.Fs = &RiceFs{}
// FindFs tries to find a rice.Box by its name and wraps in into a RiceFs.
func FindFs(name string) (*RiceFs, error) {
box, err := rice.FindBox(name)
if err != nil {
return nil, err
}
return NewFs(box), nil
}
// NewFs creates a new RiceFs based on the supplied rice.Box.
func NewFs(b *rice.Box) *RiceFs {
return &RiceFs{b}
}
// MustFindFs tries to find a rice.Box by its name and wraps in into a RiceFs. If the box is not found the method panics.
func MustFindFs(name string) *RiceFs {
box := rice.MustFindBox(name)
return NewFs(box)
}
// RiceFs represents an afero.Fs based on an underlying rice.Box.
type RiceFs struct {
Box *rice.Box
}
// ErrReadOnly is returned when a write method is accessed.
var ErrReadOnly = errors.New("file system is read-only")
// ErrUseOpen is returned when trying to use OpenFile, use Open to open the (read-only) file.
var ErrUseOpen = errors.New("use Open method instead, non-read-only perms are not supported")
// Create is not supported for a RiceFs.
func (f *RiceFs) Create(name string) (afero.File, error) {
return nil, ErrReadOnly
}
// Mkdir is not supported for a RiceFs.
func (f *RiceFs) Mkdir(name string, perm os.FileMode) error {
return ErrReadOnly
}
// MkdirAll is not supported for a RiceFs.
func (f *RiceFs) MkdirAll(path string, perm os.FileMode) error {
return ErrReadOnly
}
// Open opens a file, returning it or an error, if any happens.
func (f *RiceFs) Open(name string) (afero.File, error) {
fBox, err := f.Box.Open(name)
if nil != err {
return nil, err
}
return &file{f: fBox}, nil
}
// OpenFile is not supported for a RiceFs.
func (f *RiceFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
return nil, ErrUseOpen
}
// Remove is not supported for a RiceFs.
func (f *RiceFs) Remove(name string) error { return ErrReadOnly }
// RemoveAll is not supported for a RiceFs.
func (f *RiceFs) RemoveAll(path string) error { return ErrReadOnly }
// Rename is not supported for a RiceFs.
func (f *RiceFs) Rename(oldname, newname string) error { return ErrReadOnly }
// Stat returns a FileInfo describing the named file, or an error, if any happens.
func (f *RiceFs) Stat(name string) (os.FileInfo, error) {
fBox, err := f.Box.Open(name)
if nil != err {
return nil, err
}
return fBox.Stat()
}
// Name returns the name of the file system.
func (f *RiceFs) Name() string {
return "Rice FS"
}
// Chmod is not supported for a RiceFs.
func (f *RiceFs) Chmod(name string, mode os.FileMode) error { return ErrReadOnly }
// Chown changes the uid and gid of the named file.
func (f *RiceFs) Chown(name string, uid, gid int) error { return ErrReadOnly }
// Chtimes is not supported for a RiceFs.
func (f *RiceFs) Chtimes(name string, atime time.Time, mtime time.Time) error { return ErrReadOnly }