Package vfsutil
Package vfsutil implements some I/O utility functions for http.FileSystem.
- func ReadDir(fs http.FileSystem, name string) ([]os.FileInfo, error)
- func ReadFile(fs http.FileSystem, path string) ([]byte, error)
- func Stat(fs http.FileSystem, name string) (os.FileInfo, error)
- func Walk(fs http.FileSystem, root string, walkFn filepath.WalkFunc) error
- func WalkFiles(fs http.FileSystem, root string, walkFn WalkFilesFunc) error
- type File
- func (f File) Open(name string) (http.File, error)
- type WalkFilesFunc
Package files
file.go
vfsutil.go
walk.go
func ReadDir(fs http.FileSystem, name string) ([]os.FileInfo, error)
ReadDir reads the contents of the directory associated with file and
returns a slice of FileInfo values in directory order.
func ReadFile(fs http.FileSystem, path string) ([]byte, error)
ReadFile reads the file named by path from fs and returns the contents.
func Stat(fs http.FileSystem, name string) (os.FileInfo, error)
Stat returns the FileInfo structure describing file.
func Walk(fs http.FileSystem, root string, walkFn filepath.WalkFunc) error
Walk walks the filesystem rooted at root, calling walkFn for each file or
directory in the filesystem, including root. All errors that arise visiting files
and directories are filtered by walkFn. The files are walked in lexical
order.
▾ Example
Code:
var fs http.FileSystem = 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.",
"folderA/entry-A.txt": "Alpha.",
"folderA/entry-B.txt": "Beta.",
}))
walkFn := 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
}
err := vfsutil.Walk(fs, "/", walkFn)
if err != nil {
panic(err)
}
Output:
/
/a-file.txt
/another-file.txt
/folderA
/folderA/entry-A.txt
/folderA/entry-B.txt
/zzz-last-file.txt
func WalkFiles(fs http.FileSystem, root string, walkFn WalkFilesFunc) error
WalkFiles walks the filesystem rooted at root, calling walkFn for each file or
directory in the filesystem, including root. In addition to FileInfo, it passes an
ReadSeeker to walkFn for each file it visits.
▾ Example
Code:
var fs http.FileSystem = 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.",
"folderA/entry-A.txt": "Alpha.",
"folderA/entry-B.txt": "Beta.",
}))
walkFn := func(path string, fi os.FileInfo, r io.ReadSeeker, err error) error {
if err != nil {
log.Printf("can't stat file %s: %v\n", path, err)
return nil
}
fmt.Println(path)
if !fi.IsDir() {
b, err := io.ReadAll(r)
if err != nil {
log.Printf("can't read file %s: %v\n", path, err)
return nil
}
fmt.Printf("%q\n", b)
}
return nil
}
err := vfsutil.WalkFiles(fs, "/", walkFn)
if err != nil {
panic(err)
}
Output:
/
/a-file.txt
"It has stuff."
/another-file.txt
"Also stuff."
/folderA
/folderA/entry-A.txt
"Alpha."
/folderA/entry-B.txt
"Beta."
/zzz-last-file.txt
"It should be visited last."
File implements http.FileSystem using the native file system restricted to a
specific file served at root.
While the FileSystem.Open method takes '/'-separated paths, a File's string
value is a filename on the native file system, not a URL, so it is separated
by filepath.Separator, which isn't necessarily '/'.
type File string
func (File) Open
¶
func (f File) Open(name string) (http.File, error)
WalkFilesFunc is the type of the function called for each file or directory visited by WalkFiles.
It's like filepath.WalkFunc, except it provides an additional ReadSeeker parameter for file being visited.
type WalkFilesFunc func(path string, info os.FileInfo, rs io.ReadSeeker, err error) error