diff --git a/vfs/base_file.go b/vfs/base_file.go index 69428bb..ab3ac5f 100644 --- a/vfs/base_file.go +++ b/vfs/base_file.go @@ -1,8 +1,6 @@ package vfs import ( - "errors" - "fmt" "io" "io/ioutil" ) @@ -24,30 +22,6 @@ func (b *BaseFile) AsBytes() ([]byte, error) { return ioutil.ReadAll(b) } -func (b *BaseFile) DeleteMatching(filter FileFilter) (err error) { - var info VFileInfo - info, err = b.Info() - - if err == nil { - if info.IsDir() { - err = errors.New(fmt.Sprintf("Invalid operation DeleteMatching %s is a file", b.Url().String())) - } else { - var files []VFile - files, err = b.Find(filter) - if err == nil { - for _, file := range files { - err = file.Delete() - if err != nil { - break - } - } - } - } - } - - return -} - func (b *BaseFile) WriteString(s string) (int, error) { return io.WriteString(b, s) } diff --git a/vfs/base_fs.go b/vfs/base_fs.go index bdebfa7..da62a24 100644 --- a/vfs/base_fs.go +++ b/vfs/base_fs.go @@ -155,6 +155,18 @@ func (b *BaseVFS) OpenRaw(l string) (file VFile, err error) { return } +func (b *BaseVFS) Find(location *url.URL, filter FileFilter) (files []VFile, err error) { + err = b.Walk(location, func(file VFile) (err error) { + var filterPass bool + filterPass, err = filter(file) + if err == nil && filterPass { + files = append(files, file) + } + return + }) + return +} + func (b *BaseVFS) Walk(u *url.URL, fn WalkFn) (err error) { var src VFile var srcFi VFileInfo @@ -197,3 +209,17 @@ func (b *BaseVFS) WalkRaw(raw string, fn WalkFn) (err error) { } return } + +func (b *BaseVFS) DeleteMatching(location *url.URL, filter FileFilter) (err error) { + var files []VFile + files, err = b.Find(location, filter) + if err == nil { + for _, file := range files { + err = file.Delete() + if err != nil { + break + } + } + } + return +} diff --git a/vfs/local_file.go b/vfs/local_file.go index 008ba9f..ee40685 100644 --- a/vfs/local_file.go +++ b/vfs/local_file.go @@ -71,18 +71,6 @@ func (o *OsFile) Delete() error { return os.Remove(o.Location.Path) } -func (o *OsFile) Find(filter FileFilter) (files []VFile, err error) { - err = o.fs.Walk(o.Location, func(file VFile) (err error) { - var filterPass bool - filterPass, err = filter(file) - if err == nil && filterPass { - files = append(files, file) - } - return - }) - return -} - func (o *OsFile) Info() (VFileInfo, error) { return o.file.Stat() } diff --git a/vfs/vfile.go b/vfs/vfile.go index 92f8811..4828fb9 100644 --- a/vfs/vfile.go +++ b/vfs/vfile.go @@ -8,7 +8,7 @@ import ( type FileFilter func(file VFile) (bool, error) -//VFile interface provides the basic functions required to interact +// VFile interface provides the basic functions required to interact type VFile interface { //Closer interface included from io package io.Closer @@ -18,12 +18,6 @@ type VFile interface { ListAll() ([]VFile, error) //Delete the file object. If the file type is directory all files and subdirectories will be deleted Delete() error - //DeleteMatching will delete only the files that match the filter. - //Throws error if the files is not a dir type - //If one of the file deletion fails with an error then it stops processing and returns error - DeleteMatching(filter FileFilter) error - //Find files based on filter only works if the file.IsDir() is true - Find(filter FileFilter) ([]VFile, error) //Info Get the file ifo Info() (VFileInfo, error) //Parent of the file system @@ -36,7 +30,7 @@ type VFile interface { GetProperty(name string) (string, error) } -//VFileContent interface providers access to the content +// VFileContent interface providers access to the content type VFileContent interface { io.ReadWriteSeeker //AsString content of the file. This should be used very carefully as it is not wise to load a large file in to string diff --git a/vfs/vfs.go b/vfs/vfs.go index 9063cfe..d21d4e1 100644 --- a/vfs/vfs.go +++ b/vfs/vfs.go @@ -51,8 +51,13 @@ type VFileSystem interface { //If the URL resolves to a file it's not expected to throw an error instead the fn just be invoked with the VFile //representing the url once Walk(url *url.URL, fn WalkFn) error + //Find files based on filter only works if the file.IsDir() is true + Find(location *url.URL, filter FileFilter) ([]VFile, error) //WalkRaw is same as Walk except that it will accept the url as a string WalkRaw(raw string, fn WalkFn) error + //DeleteMatching will delete only the files that match the filter. + //If one of the file deletion fails with an error then it stops processing and returns error + DeleteMatching(location *url.URL, filter FileFilter) error } type Manager interface { diff --git a/vfs/vfs_manager.go b/vfs/vfs_manager.go index f69de5a..04e5405 100644 --- a/vfs/vfs_manager.go +++ b/vfs/vfs_manager.go @@ -237,8 +237,25 @@ func (fs *fileSystems) WalkRaw(raw string, fn WalkFn) (err error) { return } -func init() { +func (fs *fileSystems) Find(url *url.URL, filter FileFilter) (files []VFile, err error) { + var vfs VFileSystem + vfs, err = fs.getFsFor(url) + if err == nil { + files, err = vfs.Find(url, filter) + } + return +} +func (fs *fileSystems) DeleteMatching(url *url.URL, filter FileFilter) (err error) { + var vfs VFileSystem + vfs, err = fs.getFsFor(url) + if err == nil { + err = vfs.DeleteMatching(url, filter) + } + return +} + +func init() { manager = &fileSystems{} localFs := &OsFs{} manager.Register(localFs)