golang.org/x/tools@v0.21.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 "golang.org/x/tools/godoc/vfs"
     8  
     9  import (
    10  	"io"
    11  	"os"
    12  )
    13  
    14  // RootType indicates the type of files contained within a directory.
    15  //
    16  // It is used to indicate whether a directory is the root
    17  // of a GOROOT, a GOPATH, or neither.
    18  // An empty string represents the case when a directory is neither.
    19  type RootType string
    20  
    21  const (
    22  	RootTypeGoRoot RootType = "GOROOT"
    23  	RootTypeGoPath RootType = "GOPATH"
    24  )
    25  
    26  // The FileSystem interface specifies the methods godoc is using
    27  // to access the file system for which it serves documentation.
    28  type FileSystem interface {
    29  	Opener
    30  	Lstat(path string) (os.FileInfo, error)
    31  	Stat(path string) (os.FileInfo, error)
    32  	ReadDir(path string) ([]os.FileInfo, error)
    33  	RootType(path string) RootType
    34  	String() string
    35  }
    36  
    37  // Opener is a minimal virtual filesystem that can only open regular files.
    38  type Opener interface {
    39  	Open(name string) (ReadSeekCloser, error)
    40  }
    41  
    42  // A ReadSeekCloser can Read, Seek, and Close.
    43  type ReadSeekCloser interface {
    44  	io.Reader
    45  	io.Seeker
    46  	io.Closer
    47  }
    48  
    49  // ReadFile reads the file named by path from fs and returns the contents.
    50  func ReadFile(fs Opener, path string) ([]byte, error) {
    51  	rc, err := fs.Open(path)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	defer rc.Close()
    56  	return io.ReadAll(rc)
    57  }