Documentation ¶
Overview ¶
Package vfsutil implements some I/O utility functions for http.FileSystem.
Index ¶
- 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
- type WalkFilesFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReadDir ¶
ReadDir reads the contents of the directory associated with file and returns a slice of FileInfo values in directory order.
func ReadFile ¶
func ReadFile(fs http.FileSystem, path string) ([]byte, error)
ReadFile reads the file named by path from fs and returns the contents.
func Walk ¶
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 ¶
package main import ( "fmt" "log" "net/http" "os" "github.com/shurcooL/httpfs/vfsutil" "golang.org/x/tools/godoc/vfs/httpfs" "golang.org/x/tools/godoc/vfs/mapfs" ) func main() { 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 ¶
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 ¶
package main import ( "fmt" "io" "log" "net/http" "os" "github.com/shurcooL/httpfs/vfsutil" "golang.org/x/tools/godoc/vfs/httpfs" "golang.org/x/tools/godoc/vfs/mapfs" ) func main() { 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."
Types ¶
type File ¶
type File string
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 WalkFilesFunc ¶
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.