...

Source file src/github.com/shurcooL/httpfs/vfsutil/walk_test.go

Documentation: github.com/shurcooL/httpfs/vfsutil

     1  package vfsutil_test
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"log"
     7  	"net/http"
     8  	"os"
     9  
    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 ExampleWalk() {
    16  	var fs http.FileSystem = httpfs.New(mapfs.New(map[string]string{
    17  		"zzz-last-file.txt":   "It should be visited last.",
    18  		"a-file.txt":          "It has stuff.",
    19  		"another-file.txt":    "Also stuff.",
    20  		"folderA/entry-A.txt": "Alpha.",
    21  		"folderA/entry-B.txt": "Beta.",
    22  	}))
    23  
    24  	walkFn := func(path string, fi os.FileInfo, err error) error {
    25  		if err != nil {
    26  			log.Printf("can't stat file %s: %v\n", path, err)
    27  			return nil
    28  		}
    29  		fmt.Println(path)
    30  		return nil
    31  	}
    32  
    33  	err := vfsutil.Walk(fs, "/", walkFn)
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  
    38  	// Output:
    39  	// /
    40  	// /a-file.txt
    41  	// /another-file.txt
    42  	// /folderA
    43  	// /folderA/entry-A.txt
    44  	// /folderA/entry-B.txt
    45  	// /zzz-last-file.txt
    46  }
    47  
    48  func ExampleWalkFiles() {
    49  	var fs http.FileSystem = httpfs.New(mapfs.New(map[string]string{
    50  		"zzz-last-file.txt":   "It should be visited last.",
    51  		"a-file.txt":          "It has stuff.",
    52  		"another-file.txt":    "Also stuff.",
    53  		"folderA/entry-A.txt": "Alpha.",
    54  		"folderA/entry-B.txt": "Beta.",
    55  	}))
    56  
    57  	walkFn := func(path string, fi os.FileInfo, r io.ReadSeeker, err error) error {
    58  		if err != nil {
    59  			log.Printf("can't stat file %s: %v\n", path, err)
    60  			return nil
    61  		}
    62  		fmt.Println(path)
    63  		if !fi.IsDir() {
    64  			b, err := io.ReadAll(r)
    65  			if err != nil {
    66  				log.Printf("can't read file %s: %v\n", path, err)
    67  				return nil
    68  			}
    69  			fmt.Printf("%q\n", b)
    70  		}
    71  		return nil
    72  	}
    73  
    74  	err := vfsutil.WalkFiles(fs, "/", walkFn)
    75  	if err != nil {
    76  		panic(err)
    77  	}
    78  
    79  	// Output:
    80  	// /
    81  	// /a-file.txt
    82  	// "It has stuff."
    83  	// /another-file.txt
    84  	// "Also stuff."
    85  	// /folderA
    86  	// /folderA/entry-A.txt
    87  	// "Alpha."
    88  	// /folderA/entry-B.txt
    89  	// "Beta."
    90  	// /zzz-last-file.txt
    91  	// "It should be visited last."
    92  }
    93  

View as plain text