fs.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package lorca
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. )
  9. // Embed is a helper function that embeds assets from the given directories
  10. // into a Go source file. It is designed to be called from some generator
  11. // script, see example project to find out how it can be used.
  12. func Embed(packageName, file string, dirs ...string) error {
  13. w, err := os.Create(file)
  14. if err != nil {
  15. return err
  16. }
  17. defer w.Close()
  18. fmt.Fprintf(w, `// Code generated by Lorca. DO NOT EDIT.
  19. package %s
  20. import (
  21. "bytes"
  22. "errors"
  23. "net/http"
  24. "os"
  25. "time"
  26. )
  27. var assets = map[string][]byte{}
  28. var FS = &fs{}
  29. type fs struct {}
  30. func (fs *fs) Open(name string) (http.File, error) {
  31. if name == "/" {
  32. return fs, nil;
  33. }
  34. b, ok := assets[name]
  35. if !ok {
  36. return nil, os.ErrNotExist
  37. }
  38. return &file{name: name, size: len(b), Reader: bytes.NewReader(b)}, nil
  39. }
  40. func (fs *fs) Close() error { return nil }
  41. func (fs *fs) Read(p []byte) (int, error) { return 0, nil }
  42. func (fs *fs) Seek(offset int64, whence int) (int64, error) { return 0, nil }
  43. func (fs *fs) Stat() (os.FileInfo, error) { return fs, nil }
  44. func (fs *fs) Name() string { return "/" }
  45. func (fs *fs) Size() int64 { return 0 }
  46. func (fs *fs) Mode() os.FileMode { return 0755}
  47. func (fs *fs) ModTime() time.Time{ return time.Time{} }
  48. func (fs *fs) IsDir() bool { return true }
  49. func (fs *fs) Sys() interface{} { return nil }
  50. func (fs *fs) Readdir(count int) ([]os.FileInfo, error) {
  51. files := []os.FileInfo{}
  52. for name, data := range assets {
  53. files = append(files, &file{name: name, size: len(data), Reader: bytes.NewReader(data)})
  54. }
  55. return files, nil
  56. }
  57. type file struct {
  58. name string
  59. size int
  60. *bytes.Reader
  61. }
  62. func (f *file) Close() error { return nil }
  63. func (f *file) Readdir(count int) ([]os.FileInfo, error) { return nil, errors.New("not supported") }
  64. func (f *file) Stat() (os.FileInfo, error) { return f, nil }
  65. func (f *file) Name() string { return f.name }
  66. func (f *file) Size() int64 { return int64(f.size) }
  67. func (f *file) Mode() os.FileMode { return 0644 }
  68. func (f *file) ModTime() time.Time{ return time.Time{} }
  69. func (f *file) IsDir() bool { return false }
  70. func (f *file) Sys() interface{} { return nil }
  71. func init() {
  72. `, packageName)
  73. defer fmt.Fprintln(w, `}`)
  74. for _, dir := range dirs {
  75. filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  76. if info.IsDir() {
  77. return nil
  78. }
  79. b, err := ioutil.ReadFile(path)
  80. if err != nil {
  81. return err
  82. }
  83. path = filepath.ToSlash(path)
  84. fmt.Fprintf(w, ` assets[%q] = []byte{`, strings.TrimPrefix(path, dir))
  85. for i := 0; i < len(b); i++ {
  86. if i > 0 {
  87. fmt.Fprintf(w, `, `)
  88. }
  89. fmt.Fprintf(w, `0x%02x`, b[i])
  90. }
  91. fmt.Fprintln(w, `}`)
  92. return nil
  93. })
  94. }
  95. return nil
  96. }