github.com/v2fly/tools@v0.100.0/godoc/vfs/vfs.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package vfs defines types for abstract file system access and provides an 6 // implementation accessing the file system of the underlying OS. 7 package vfs // import "github.com/v2fly/tools/godoc/vfs" 8 9 import ( 10 "io" 11 "io/ioutil" 12 "os" 13 ) 14 15 // RootType indicates the type of files contained within a directory. 16 // 17 // It is used to indicate whether a directory is the root 18 // of a GOROOT, a GOPATH, or neither. 19 // An empty string represents the case when a directory is neither. 20 type RootType string 21 22 const ( 23 RootTypeGoRoot RootType = "GOROOT" 24 RootTypeGoPath RootType = "GOPATH" 25 ) 26 27 // The FileSystem interface specifies the methods godoc is using 28 // to access the file system for which it serves documentation. 29 type FileSystem interface { 30 Opener 31 Lstat(path string) (os.FileInfo, error) 32 Stat(path string) (os.FileInfo, error) 33 ReadDir(path string) ([]os.FileInfo, error) 34 RootType(path string) RootType 35 String() string 36 } 37 38 // Opener is a minimal virtual filesystem that can only open regular files. 39 type Opener interface { 40 Open(name string) (ReadSeekCloser, error) 41 } 42 43 // A ReadSeekCloser can Read, Seek, and Close. 44 type ReadSeekCloser interface { 45 io.Reader 46 io.Seeker 47 io.Closer 48 } 49 50 // ReadFile reads the file named by path from fs and returns the contents. 51 func ReadFile(fs Opener, path string) ([]byte, error) { 52 rc, err := fs.Open(path) 53 if err != nil { 54 return nil, err 55 } 56 defer rc.Close() 57 return ioutil.ReadAll(rc) 58 }