Documentation

Overview

Package filter offers an http.FileSystem wrapper with the ability to keep or skip files.

Example (Detailed)
package main

import (
	"fmt"
	"io"
	"log"
	"os"
	pathpkg "path"

	"github.com/shurcooL/httpfs/filter"
	"github.com/shurcooL/httpfs/vfsutil"
	"golang.org/x/tools/godoc/vfs/httpfs"
	"golang.org/x/tools/godoc/vfs/mapfs"
)

func main() {
	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.",
	}))

	// Skip files with .go and .html extensions, and directories named "folder-to-skip" (but
	// not files named "folder-to-skip").
	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()

	// This file should be filtered out, even if accessed directly.
	_, err = fs.Open("/folderA/main.go")
	fmt.Println("os.IsNotExist(err):", os.IsNotExist(err))
	fmt.Println(err)

	fmt.Println()

	// This folder should be filtered out, even if accessed directly.
	_, err = fs.Open("/folderA/folder-to-skip")
	fmt.Println("os.IsNotExist(err):", os.IsNotExist(err))
	fmt.Println(err)

	fmt.Println()

	// This file should not be filtered out.
	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.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Keep

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
package main

import (
	"net/http"
	"os"
	"strings"

	"github.com/shurcooL/httpfs/filter"
)

func main() {
	var srcFS http.FileSystem

	// Keep only "/target/dir" and its contents.
	fs := filter.Keep(srcFS, func(path string, fi os.FileInfo) bool {
		return path == "/" ||
			path == "/target" ||
			path == "/target/dir" ||
			strings.HasPrefix(path, "/target/dir/")
	})

	_ = fs
}
Output:

func Skip

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
package main

import (
	"net/http"
	"os"

	"github.com/shurcooL/httpfs/filter"
)

func main() {
	var srcFS http.FileSystem

	// Skip all files named ".DS_Store".
	fs := filter.Skip(srcFS, func(path string, fi os.FileInfo) bool {
		return !fi.IsDir() && fi.Name() == ".DS_Store"
	})

	_ = fs
}
Output:

Types

type Func

type Func func(path string, fi os.FileInfo) bool

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".

func FilesWithExtensions

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.