Example #1
0
func ExampleWalk() {
	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
}
Example #2
0
func ExampleEmpty() {
	empty := union.New(nil)

	err := vfsutil.Walk(empty, "/", walk)
	if err != nil {
		panic(err)
	}

	// Output:
	// /
}
Example #3
0
func Example() {
	fs0 := 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.",
	}))
	fs1 := httpfs.New(mapfs.New(map[string]string{
		"sample-file.txt":                "This file compresses well. Blaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah!",
		"not-worth-compressing-file.txt": "Its normal contents are here.",
		"folderA/file1.txt":              "Stuff 1.",
		"folderA/file2.txt":              "Stuff 2.",
		"folderB/folderC/file3.txt":      "Stuff C-3.",
	}))

	fs := union.New(map[string]http.FileSystem{
		"/fs0": fs0,
		"/fs1": fs1,
	})

	err := vfsutil.Walk(fs, "/", walk)
	if err != nil {
		panic(err)
	}

	// Output:
	// /
	// /fs0
	// /fs0/a-file.txt
	// /fs0/another-file.txt
	// /fs0/folderA
	// /fs0/folderA/entry-A.txt
	// /fs0/folderA/entry-B.txt
	// /fs0/zzz-last-file.txt
	// /fs1
	// /fs1/folderA
	// /fs1/folderA/file1.txt
	// /fs1/folderA/file2.txt
	// /fs1/folderB
	// /fs1/folderB/folderC
	// /fs1/folderB/folderC/file3.txt
	// /fs1/not-worth-compressing-file.txt
	// /fs1/sample-file.txt
}
Example #4
0
func Example() {
	walk := 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
	}

	fs := 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.",
	}))

	ignore := func(fi os.FileInfo, _ string) bool {
		return pathpkg.Ext(fi.Name()) == ".go" || pathpkg.Ext(fi.Name()) == ".html" ||
			(fi.IsDir() && fi.Name() == "folder-to-skip")
	}

	fs = filter.NewIgnore(fs, ignore)

	err := vfsutil.Walk(fs, "/", walk)
	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.
}