Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
Merge branch 'feat/#71' into feat/vfs-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
neo7337 committed Apr 16, 2023
2 parents e86cda7 + d830b37 commit 30dfcff
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 47 deletions.
26 changes: 0 additions & 26 deletions vfs/base_file.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package vfs

import (
"errors"
"fmt"
"io"
"io/ioutil"
)
Expand All @@ -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)
}
26 changes: 26 additions & 0 deletions vfs/base_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
12 changes: 0 additions & 12 deletions vfs/local_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
10 changes: 2 additions & 8 deletions vfs/vfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions vfs/vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 18 additions & 1 deletion vfs/vfs_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,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)
Expand Down

0 comments on commit 30dfcff

Please sign in to comment.