You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2 KiB

  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. // Package vfs defines types for abstract file system access and provides an
  5. // implementation accessing the file system of the underlying OS.
  6. package vfs // import "golang.org/x/tools/godoc/vfs"
  7. import (
  8. "io"
  9. "io/ioutil"
  10. "os"
  11. )
  12. // The FileSystem interface specifies the methods godoc is using
  13. // to access the file system for which it serves documentation.
  14. type FileSystem interface {
  15. Opener
  16. Lstat(path string) (os.FileInfo, error)
  17. Stat(path string) (os.FileInfo, error)
  18. ReadDir(path string) ([]os.FileInfo, error)
  19. String() string
  20. }
  21. // Opener is a minimal virtual filesystem that can only open regular files.
  22. type Opener interface {
  23. Open(name string) (ReadSeekCloser, error)
  24. }
  25. // A ReadSeekCloser can Read, Seek, and Close.
  26. type ReadSeekCloser interface {
  27. io.Reader
  28. io.Seeker
  29. io.Closer
  30. }
  31. // ReadFile reads the file named by path from fs and returns the contents.
  32. func ReadFile(fs Opener, path string) ([]byte, error) {
  33. rc, err := fs.Open(path)
  34. if err != nil {
  35. return nil, err
  36. }
  37. defer rc.Close()
  38. return ioutil.ReadAll(rc)
  39. }