...
Package filter
Package filter offers an http.FileSystem wrapper with the ability to keep or skip files.
▾ Example (Detailed)
Code:
srcFS := httpfs.New(mapfs.New(map[string]string{
"zzz-last-file.txt": "It should be visited last.",
"a-file.txt": "It has stuff.",
"another-file.txt": "Also stuff.",
"some-file.html": "<html>and stuff</html>",
"folderA/entry-A.txt": "Alpha.",
"folderA/entry-B.txt": "Beta.",
"folderA/main.go": "package main\n",
"folderA/folder-to-skip/many.txt": "Entire folder can be skipped.",
"folderA/folder-to-skip/files.txt": "Entire folder can be skipped.",
"folder-to-skip": "This is a file, not a folder, and shouldn't be skipped.",
}))
fs := filter.Skip(srcFS, func(path string, fi os.FileInfo) bool {
return pathpkg.Ext(fi.Name()) == ".go" || pathpkg.Ext(fi.Name()) == ".html" ||
(fi.IsDir() && fi.Name() == "folder-to-skip")
})
err := vfsutil.Walk(fs, "/", func(path string, fi os.FileInfo, err error) error {
if err != nil {
log.Printf("can't stat file %s: %v\n", path, err)
return nil
}
fmt.Println(path)
return nil
})
if err != nil {
panic(err)
}
fmt.Println()
_, err = fs.Open("/folderA/main.go")
fmt.Println("os.IsNotExist(err):", os.IsNotExist(err))
fmt.Println(err)
fmt.Println()
_, err = fs.Open("/folderA/folder-to-skip")
fmt.Println("os.IsNotExist(err):", os.IsNotExist(err))
fmt.Println(err)
fmt.Println()
f, err := fs.Open("/folder-to-skip")
if err != nil {
panic(err)
}
io.Copy(os.Stdout, f)
f.Close()
Output:
/
/a-file.txt
/another-file.txt
/folder-to-skip
/folderA
/folderA/entry-A.txt
/folderA/entry-B.txt
/zzz-last-file.txt
os.IsNotExist(err): true
open /folderA/main.go: file does not exist
os.IsNotExist(err): true
open /folderA/folder-to-skip: file does not exist
This is a file, not a folder, and shouldn't be skipped.
func Keep(source http.FileSystem, keep Func) http.FileSystem
Keep returns a filesystem that contains only those entries in source for which
keep returns true.
▾ Example
Code:
var srcFS http.FileSystem
fs := filter.Keep(srcFS, func(path string, fi os.FileInfo) bool {
return path == "/" ||
path == "/target" ||
path == "/target/dir" ||
strings.HasPrefix(path, "/target/dir/")
})
_ = fs
func Skip(source http.FileSystem, skip Func) http.FileSystem
Skip returns a filesystem that contains everything in source, except entries
for which skip returns true.
▾ Example
Code:
var srcFS http.FileSystem
fs := filter.Skip(srcFS, func(path string, fi os.FileInfo) bool {
return !fi.IsDir() && fi.Name() == ".DS_Store"
})
_ = fs
Func is a selection function which is provided two arguments,
its '/'-separated cleaned rooted absolute path (i.e., it always begins with "/"),
and the os.FileInfo of the considered file.
The path is cleaned via pathpkg.Clean("/" + path).
For example, if the considered file is named "a" and it's inside a directory "dir",
then the value of path will be "/dir/a".
type Func func(path string, fi os.FileInfo) bool
func FilesWithExtensions(exts ...string) Func
FilesWithExtensions returns a filter func that selects files (but not directories)
that have any of the given extensions. For example:
filter.FilesWithExtensions(".go", ".html")
Would select both .go and .html files. It would not select any directories.