httpfs: github.com/shurcooL/httpfs/vfsutil Index | Examples | Files

package vfsutil

import "github.com/shurcooL/httpfs/vfsutil"

Package vfsutil implements some I/O utility functions for http.FileSystem.

Index

Examples

Package Files

file.go vfsutil.go walk.go

func ReadDir Uses

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 Uses

func ReadFile(fs http.FileSystem, path string) ([]byte, error)

ReadFile reads the file named by path from fs and returns the contents.

func Stat Uses

func Stat(fs http.FileSystem, name string) (os.FileInfo, error)

Stat returns the FileInfo structure describing file.

func Walk Uses

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.

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 Uses

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.

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 := ioutil.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."

type File Uses

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 '/'.

func (File) Open Uses

func (f File) Open(name string) (http.File, error)

type WalkFilesFunc Uses

type WalkFilesFunc func(path string, info os.FileInfo, rs io.ReadSeeker, err error) 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.

Package vfsutil imports 8 packages (graph) and is imported by 9 packages. Updated 2017-05-12. Refresh now. Tools for package owners.