fs.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package compiler
  2. import (
  3. "os"
  4. "io"
  5. "fmt"
  6. "embed"
  7. "io/fs"
  8. "errors"
  9. "archive/zip"
  10. "path/filepath"
  11. )
  12. type FileSystem interface {
  13. Open(path string) (File, error)
  14. Key() string
  15. }
  16. type File interface {
  17. Close() error
  18. ReadContent() ([] byte, error)
  19. }
  20. func ReadFile(path string, fs FileSystem) ([] byte, error) {
  21. var f, err = fs.Open(path)
  22. if err != nil { return nil, err }
  23. { var content, err = f.ReadContent()
  24. if err != nil { return nil, err }
  25. { var err = f.Close()
  26. if err != nil { return nil, err }
  27. return content, nil } }
  28. }
  29. type RealFileSystem struct {}
  30. type RealFile struct {
  31. Fd *os.File
  32. }
  33. func (_ RealFileSystem) Open(path string) (File, error) {
  34. var fd, err = os.Open(path)
  35. if err != nil { return nil, err }
  36. return RealFile{fd}, nil
  37. }
  38. func (_ RealFileSystem) Key() string {
  39. return ""
  40. }
  41. func (f RealFile) Close() error {
  42. return f.Fd.Close()
  43. }
  44. func (f RealFile) ReadContent() ([] byte, error) {
  45. return io.ReadAll(f.Fd)
  46. }
  47. type InlineFileSystem struct {
  48. Id string
  49. Files map[string] ([] byte)
  50. }
  51. type InlineFile struct {
  52. Content [] byte
  53. }
  54. func (fs InlineFileSystem) Open(path string) (File, error) {
  55. var content, exists = fs.Files[path]
  56. if !(exists) {
  57. return nil, errors.New("no such file: " + path)
  58. }
  59. return InlineFile{content}, nil
  60. }
  61. func (fs InlineFileSystem) Key() string {
  62. return fmt.Sprintf("(inline.%s)", fs.Id)
  63. }
  64. func (f InlineFile) Close() error {
  65. return nil
  66. }
  67. func (f InlineFile) ReadContent() ([] byte, error) {
  68. return f.Content, nil
  69. }
  70. type EmbeddedFileSystem struct {
  71. Id string
  72. FS embed.FS
  73. }
  74. type StdFile struct {
  75. File fs.File
  76. }
  77. func (fs EmbeddedFileSystem) Open(path string) (File, error) {
  78. var f, err = fs.FS.Open(filepath.ToSlash(path))
  79. if err != nil { return nil, err }
  80. return StdFile{f}, nil
  81. }
  82. func (fs EmbeddedFileSystem) Key() string {
  83. return fmt.Sprintf("(embedded.%s)", fs.Id)
  84. }
  85. func (f StdFile) Close() error {
  86. return f.File.Close()
  87. }
  88. func (f StdFile) ReadContent() ([]byte, error) {
  89. return io.ReadAll(f.File)
  90. }
  91. type ZipFilesystem struct {
  92. Id string
  93. Reader *zip.Reader
  94. }
  95. func (fs ZipFilesystem) Open(path string) (File, error) {
  96. var f, err = fs.Reader.Open(filepath.ToSlash(path))
  97. if err != nil { return nil, err }
  98. return StdFile{f}, nil
  99. }
  100. func (fs ZipFilesystem) Key() string {
  101. return fmt.Sprintf("(zip.%s)", fs.Id)
  102. }