vfsutil

package
v0.0.0-...-f1e31cf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2023 License: MIT Imports: 6 Imported by: 95

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadDir

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

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

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

func Stat

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

Stat returns the FileInfo structure describing file.

func Walk

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

func (File) Open

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

type WalkFilesFunc

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL