2013-07-16 22:44:18 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-07-16 23:02:27 -06:00
|
|
|
// Package vfs defines types for abstract file system access and provides an
|
|
|
|
// implementation accessing the file system of the underlying OS.
|
2014-12-08 21:00:58 -07:00
|
|
|
package vfs // import "golang.org/x/tools/godoc/vfs"
|
2013-07-16 22:44:18 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2013-07-17 01:09:54 -06:00
|
|
|
"io/ioutil"
|
2013-07-16 23:02:27 -06:00
|
|
|
"os"
|
2013-07-16 22:44:18 -06:00
|
|
|
)
|
|
|
|
|
2018-01-28 07:23:55 -07:00
|
|
|
// RootType indicates the type of files contained within a directory.
|
|
|
|
//
|
2018-04-10 14:38:02 -06:00
|
|
|
// It is used to indicate whether a directory is the root
|
|
|
|
// of a GOROOT, a GOPATH, or neither.
|
|
|
|
// An empty string represents the case when a directory is neither.
|
2018-01-28 07:23:55 -07:00
|
|
|
type RootType string
|
|
|
|
|
|
|
|
const (
|
2018-04-10 14:38:02 -06:00
|
|
|
RootTypeGoRoot RootType = "GOROOT"
|
|
|
|
RootTypeGoPath RootType = "GOPATH"
|
2018-01-28 07:23:55 -07:00
|
|
|
)
|
|
|
|
|
2013-07-16 23:02:27 -06:00
|
|
|
// The FileSystem interface specifies the methods godoc is using
|
|
|
|
// to access the file system for which it serves documentation.
|
|
|
|
type FileSystem interface {
|
|
|
|
Opener
|
|
|
|
Lstat(path string) (os.FileInfo, error)
|
|
|
|
Stat(path string) (os.FileInfo, error)
|
|
|
|
ReadDir(path string) ([]os.FileInfo, error)
|
2018-01-28 07:23:55 -07:00
|
|
|
RootType(path string) RootType
|
2013-07-16 23:02:27 -06:00
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opener is a minimal virtual filesystem that can only open regular files.
|
2013-07-16 22:44:18 -06:00
|
|
|
type Opener interface {
|
|
|
|
Open(name string) (ReadSeekCloser, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A ReadSeekCloser can Read, Seek, and Close.
|
|
|
|
type ReadSeekCloser interface {
|
|
|
|
io.Reader
|
|
|
|
io.Seeker
|
|
|
|
io.Closer
|
|
|
|
}
|
2013-07-17 01:09:54 -06:00
|
|
|
|
|
|
|
// ReadFile reads the file named by path from fs and returns the contents.
|
|
|
|
func ReadFile(fs Opener, path string) ([]byte, error) {
|
|
|
|
rc, err := fs.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rc.Close()
|
|
|
|
return ioutil.ReadAll(rc)
|
|
|
|
}
|