debug.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
  2. // license. Its contents can be found at:
  3. // http://creativecommons.org/publicdomain/zero/1.0/
  4. package bindata
  5. import (
  6. "fmt"
  7. "io"
  8. )
  9. // writeDebug writes the debug code file.
  10. func writeDebug(w io.Writer, c *Config, toc []Asset) error {
  11. err := writeDebugHeader(w)
  12. if err != nil {
  13. return err
  14. }
  15. for i := range toc {
  16. err = writeDebugAsset(w, c, &toc[i])
  17. if err != nil {
  18. return err
  19. }
  20. }
  21. return nil
  22. }
  23. // writeDebugHeader writes output file headers.
  24. // This targets debug builds.
  25. func writeDebugHeader(w io.Writer) error {
  26. _, err := fmt.Fprintf(w, `import (
  27. "fmt"
  28. "io/ioutil"
  29. "os"
  30. "path/filepath"
  31. "strings"
  32. )
  33. // bindataRead reads the given file from disk. It returns an error on failure.
  34. func bindataRead(path, name string) ([]byte, error) {
  35. buf, err := ioutil.ReadFile(path)
  36. if err != nil {
  37. err = fmt.Errorf("Error reading asset %%s at %%s: %%v", name, path, err)
  38. }
  39. return buf, err
  40. }
  41. type asset struct {
  42. bytes []byte
  43. info os.FileInfo
  44. }
  45. `)
  46. return err
  47. }
  48. // writeDebugAsset write a debug entry for the given asset.
  49. // A debug entry is simply a function which reads the asset from
  50. // the original file (e.g.: from disk).
  51. func writeDebugAsset(w io.Writer, c *Config, asset *Asset) error {
  52. pathExpr := fmt.Sprintf("%q", asset.Path)
  53. if c.Dev {
  54. pathExpr = fmt.Sprintf("filepath.Join(rootDir, %q)", asset.Name)
  55. }
  56. _, err := fmt.Fprintf(w, `// %s reads file data from disk. It returns an error on failure.
  57. func %s() (*asset, error) {
  58. path := %s
  59. name := %q
  60. bytes, err := bindataRead(path, name)
  61. if err != nil {
  62. return nil, err
  63. }
  64. fi, err := os.Stat(path)
  65. if err != nil {
  66. err = fmt.Errorf("Error reading asset info %%s at %%s: %%v", name, path, err)
  67. }
  68. a := &asset{bytes: bytes, info: fi}
  69. return a, err
  70. }
  71. `, asset.Func, asset.Func, pathExpr, asset.Name)
  72. return err
  73. }