...
1 package union_test
2
3 import (
4 "fmt"
5 "log"
6 "net/http"
7 "os"
8
9 "github.com/shurcooL/httpfs/union"
10 "github.com/shurcooL/httpfs/vfsutil"
11 "golang.org/x/tools/godoc/vfs/httpfs"
12 "golang.org/x/tools/godoc/vfs/mapfs"
13 )
14
15 func walk(path string, fi os.FileInfo, err error) error {
16 if err != nil {
17 log.Printf("can't stat file %s: %v\n", path, err)
18 return nil
19 }
20 fmt.Println(path)
21 return nil
22 }
23
24 func Example() {
25 fs0 := httpfs.New(mapfs.New(map[string]string{
26 "zzz-last-file.txt": "It should be visited last.",
27 "a-file.txt": "It has stuff.",
28 "another-file.txt": "Also stuff.",
29 "folderA/entry-A.txt": "Alpha.",
30 "folderA/entry-B.txt": "Beta.",
31 }))
32 fs1 := httpfs.New(mapfs.New(map[string]string{
33 "sample-file.txt": "This file compresses well. Blaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah!",
34 "not-worth-compressing-file.txt": "Its normal contents are here.",
35 "folderA/file1.txt": "Stuff 1.",
36 "folderA/file2.txt": "Stuff 2.",
37 "folderB/folderC/file3.txt": "Stuff C-3.",
38 }))
39
40 fs := union.New(map[string]http.FileSystem{
41 "/fs0": fs0,
42 "/fs1": fs1,
43 })
44
45 err := vfsutil.Walk(fs, "/", walk)
46 if err != nil {
47 panic(err)
48 }
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 }
69
70 func Example_empty() {
71 empty := union.New(nil)
72
73 err := vfsutil.Walk(empty, "/", walk)
74 if err != nil {
75 panic(err)
76 }
77
78
79
80 }
81
82 func Example_notExist() {
83 empty := union.New(nil)
84
85 _, err := empty.Open("/does-not-exist")
86 fmt.Println("os.IsNotExist:", os.IsNotExist(err))
87 fmt.Println(err)
88
89
90
91
92 }
93
View as plain text