github.com/posener/gitfs@v1.2.2-0.20200410105819-ea4e48d73ab9/fsutil/walk.go (about)

     1  // Package fsutil provides useful utility functions for http.FileSystem.
     2  package fsutil
     3  
     4  import (
     5  	"net/http"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/kr/fs"
    10  )
    11  
    12  // Walk returns a fs.Walker over http.FileSystem, which enables
    13  // walking over all files in the filesystem.
    14  //
    15  // See https://godoc.org/github.com/kr/fs#Walker for more details.
    16  func Walk(hfs http.FileSystem, root string) *fs.Walker {
    17  	return fs.WalkFS(root, fileSystem{hfs})
    18  }
    19  
    20  // FileSystem implements fs.FileSystem over http.FileSystem.
    21  //
    22  // See https://godoc.org/github.com/kr/fs#FileSystem for more details.
    23  type fileSystem struct {
    24  	http.FileSystem
    25  }
    26  
    27  func (fs fileSystem) ReadDir(dirname string) ([]os.FileInfo, error) {
    28  	f, err := fs.Open(dirname)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	return f.Readdir(-1)
    33  }
    34  
    35  func (fs fileSystem) Lstat(name string) (os.FileInfo, error) {
    36  	f, err := fs.Open(name)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	return f.Stat()
    41  }
    42  
    43  func (fileSystem) Join(elem ...string) string {
    44  	return filepath.Join(elem...)
    45  }