config.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. "os"
  8. "path/filepath"
  9. "regexp"
  10. )
  11. // InputConfig defines options on a asset directory to be convert.
  12. type InputConfig struct {
  13. // Path defines a directory containing asset files to be included
  14. // in the generated output.
  15. Path string
  16. // Recursive defines whether subdirectories of Path
  17. // should be recursively included in the conversion.
  18. Recursive bool
  19. }
  20. // Config defines a set of options for the asset conversion.
  21. type Config struct {
  22. // Name of the package to use. Defaults to 'main'.
  23. Package string
  24. // Tags specify a set of optional build tags, which should be
  25. // included in the generated output. The tags are appended to a
  26. // `// +build` line in the beginning of the output file
  27. // and must follow the build tags syntax specified by the go tool.
  28. Tags string
  29. // Input defines the directory path, containing all asset files as
  30. // well as whether to recursively process assets in any sub directories.
  31. Input []InputConfig
  32. // Output defines the output file for the generated code.
  33. // If left empty, this defaults to 'bindata.go' in the current
  34. // working directory.
  35. Output string
  36. // Prefix defines a path prefix which should be stripped from all
  37. // file names when generating the keys in the table of contents.
  38. // For example, running without the `-prefix` flag, we get:
  39. //
  40. // $ go-bindata /path/to/templates
  41. // go_bindata["/path/to/templates/foo.html"] = _path_to_templates_foo_html
  42. //
  43. // Running with the `-prefix` flag, we get:
  44. //
  45. // $ go-bindata -prefix "/path/to/" /path/to/templates/foo.html
  46. // go_bindata["templates/foo.html"] = templates_foo_html
  47. Prefix string
  48. // NoMemCopy will alter the way the output file is generated.
  49. //
  50. // It will employ a hack that allows us to read the file data directly from
  51. // the compiled program's `.rodata` section. This ensures that when we call
  52. // call our generated function, we omit unnecessary mem copies.
  53. //
  54. // The downside of this, is that it requires dependencies on the `reflect` and
  55. // `unsafe` packages. These may be restricted on platforms like AppEngine and
  56. // thus prevent you from using this mode.
  57. //
  58. // Another disadvantage is that the byte slice we create, is strictly read-only.
  59. // For most use-cases this is not a problem, but if you ever try to alter the
  60. // returned byte slice, a runtime panic is thrown. Use this mode only on target
  61. // platforms where memory constraints are an issue.
  62. //
  63. // The default behaviour is to use the old code generation method. This
  64. // prevents the two previously mentioned issues, but will employ at least one
  65. // extra memcopy and thus increase memory requirements.
  66. //
  67. // For instance, consider the following two examples:
  68. //
  69. // This would be the default mode, using an extra memcopy but gives a safe
  70. // implementation without dependencies on `reflect` and `unsafe`:
  71. //
  72. // func myfile() []byte {
  73. // return []byte{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a}
  74. // }
  75. //
  76. // Here is the same functionality, but uses the `.rodata` hack.
  77. // The byte slice returned from this example can not be written to without
  78. // generating a runtime error.
  79. //
  80. // var _myfile = "\x89\x50\x4e\x47\x0d\x0a\x1a"
  81. //
  82. // func myfile() []byte {
  83. // var empty [0]byte
  84. // sx := (*reflect.StringHeader)(unsafe.Pointer(&_myfile))
  85. // b := empty[:]
  86. // bx := (*reflect.SliceHeader)(unsafe.Pointer(&b))
  87. // bx.Data = sx.Data
  88. // bx.Len = len(_myfile)
  89. // bx.Cap = bx.Len
  90. // return b
  91. // }
  92. NoMemCopy bool
  93. // NoCompress means the assets are /not/ GZIP compressed before being turned
  94. // into Go code. The generated function will automatically unzip
  95. // the file data when called. Defaults to false.
  96. NoCompress bool
  97. // HttpFileSystem means whether generate return http.FileSystem interface
  98. // instance's function.When true,will generate relate code.
  99. HttpFileSystem bool
  100. // Perform a debug build. This generates an asset file, which
  101. // loads the asset contents directly from disk at their original
  102. // location, instead of embedding the contents in the code.
  103. //
  104. // This is mostly useful if you anticipate that the assets are
  105. // going to change during your development cycle. You will always
  106. // want your code to access the latest version of the asset.
  107. // Only in release mode, will the assets actually be embedded
  108. // in the code. The default behaviour is Release mode.
  109. Debug bool
  110. // Perform a dev build, which is nearly identical to the debug option. The
  111. // only difference is that instead of absolute file paths in generated code,
  112. // it expects a variable, `rootDir`, to be set in the generated code's
  113. // package (the author needs to do this manually), which it then prepends to
  114. // an asset's name to construct the file path on disk.
  115. //
  116. // This is mainly so you can push the generated code file to a shared
  117. // repository.
  118. Dev bool
  119. // When true, size, mode and modtime are not preserved from files
  120. NoMetadata bool
  121. // When nonzero, use this as mode for all files.
  122. Mode uint
  123. // When nonzero, use this as unix timestamp for all files.
  124. ModTime int64
  125. // Ignores any filenames matching the regex pattern specified, e.g.
  126. // path/to/file.ext will ignore only that file, or \\.gitignore
  127. // will match any .gitignore file.
  128. //
  129. // This parameter can be provided multiple times.
  130. Ignore []*regexp.Regexp
  131. }
  132. // NewConfig returns a default configuration struct.
  133. func NewConfig() *Config {
  134. c := new(Config)
  135. c.Package = "main"
  136. c.NoMemCopy = false
  137. c.NoCompress = false
  138. c.HttpFileSystem = false
  139. c.Debug = false
  140. c.Output = "./bindata.go"
  141. c.Ignore = make([]*regexp.Regexp, 0)
  142. return c
  143. }
  144. // validate ensures the config has sane values.
  145. // Part of which means checking if certain file/directory paths exist.
  146. func (c *Config) validate() error {
  147. if len(c.Package) == 0 {
  148. return fmt.Errorf("missing package name")
  149. }
  150. for _, input := range c.Input {
  151. _, err := os.Lstat(input.Path)
  152. if err != nil {
  153. return fmt.Errorf("failed to stat input path '%s': %v", input.Path, err)
  154. }
  155. }
  156. if len(c.Output) == 0 {
  157. cwd, err := os.Getwd()
  158. if err != nil {
  159. return fmt.Errorf("unable to determine current working directory")
  160. }
  161. c.Output = filepath.Join(cwd, "bindata.go")
  162. }
  163. stat, err := os.Lstat(c.Output)
  164. if err != nil {
  165. if !os.IsNotExist(err) {
  166. return fmt.Errorf("output path: %v", err)
  167. }
  168. // File does not exist. This is fine, just make
  169. // sure the directory it is to be in exists.
  170. dir, _ := filepath.Split(c.Output)
  171. if dir != "" {
  172. err = os.MkdirAll(dir, 0744)
  173. if err != nil {
  174. return fmt.Errorf("create output directory: %v", err)
  175. }
  176. }
  177. }
  178. if stat != nil && stat.IsDir() {
  179. return fmt.Errorf("output path is a directory")
  180. }
  181. return nil
  182. }