...
1 package filter_test
2
3 import (
4 "fmt"
5 "io"
6 "log"
7 "net/http"
8 "os"
9 pathpkg "path"
10 "strings"
11
12 "github.com/shurcooL/httpfs/filter"
13 "github.com/shurcooL/httpfs/vfsutil"
14 "golang.org/x/tools/godoc/vfs/httpfs"
15 "golang.org/x/tools/godoc/vfs/mapfs"
16 )
17
18 func ExampleKeep() {
19 var srcFS http.FileSystem
20
21
22 fs := filter.Keep(srcFS, func(path string, fi os.FileInfo) bool {
23 return path == "/" ||
24 path == "/target" ||
25 path == "/target/dir" ||
26 strings.HasPrefix(path, "/target/dir/")
27 })
28
29 _ = fs
30 }
31
32 func ExampleSkip() {
33 var srcFS http.FileSystem
34
35
36 fs := filter.Skip(srcFS, func(path string, fi os.FileInfo) bool {
37 return !fi.IsDir() && fi.Name() == ".DS_Store"
38 })
39
40 _ = fs
41 }
42
43 func Example_detailed() {
44 srcFS := httpfs.New(mapfs.New(map[string]string{
45 "zzz-last-file.txt": "It should be visited last.",
46 "a-file.txt": "It has stuff.",
47 "another-file.txt": "Also stuff.",
48 "some-file.html": "<html>and stuff</html>",
49 "folderA/entry-A.txt": "Alpha.",
50 "folderA/entry-B.txt": "Beta.",
51 "folderA/main.go": "package main\n",
52 "folderA/folder-to-skip/many.txt": "Entire folder can be skipped.",
53 "folderA/folder-to-skip/files.txt": "Entire folder can be skipped.",
54 "folder-to-skip": "This is a file, not a folder, and shouldn't be skipped.",
55 }))
56
57
58
59 fs := filter.Skip(srcFS, func(path string, fi os.FileInfo) bool {
60 return pathpkg.Ext(fi.Name()) == ".go" || pathpkg.Ext(fi.Name()) == ".html" ||
61 (fi.IsDir() && fi.Name() == "folder-to-skip")
62 })
63
64 err := vfsutil.Walk(fs, "/", func(path string, fi os.FileInfo, err error) error {
65 if err != nil {
66 log.Printf("can't stat file %s: %v\n", path, err)
67 return nil
68 }
69 fmt.Println(path)
70 return nil
71 })
72 if err != nil {
73 panic(err)
74 }
75
76 fmt.Println()
77
78
79 _, err = fs.Open("/folderA/main.go")
80 fmt.Println("os.IsNotExist(err):", os.IsNotExist(err))
81 fmt.Println(err)
82
83 fmt.Println()
84
85
86 _, err = fs.Open("/folderA/folder-to-skip")
87 fmt.Println("os.IsNotExist(err):", os.IsNotExist(err))
88 fmt.Println(err)
89
90 fmt.Println()
91
92
93 f, err := fs.Open("/folder-to-skip")
94 if err != nil {
95 panic(err)
96 }
97 io.Copy(os.Stdout, f)
98 f.Close()
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 }
118
View as plain text