config.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // Recusive 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. // Perform a debug build. This generates an asset file, which
  98. // loads the asset contents directly from disk at their original
  99. // location, instead of embedding the contents in the code.
  100. //
  101. // This is mostly useful if you anticipate that the assets are
  102. // going to change during your development cycle. You will always
  103. // want your code to access the latest version of the asset.
  104. // Only in release mode, will the assets actually be embedded
  105. // in the code. The default behaviour is Release mode.
  106. Debug bool
  107. // Perform a dev build, which is nearly identical to the debug option. The
  108. // only difference is that instead of absolute file paths in generated code,
  109. // it expects a variable, `rootDir`, to be set in the generated code's
  110. // package (the author needs to do this manually), which it then prepends to
  111. // an asset's name to construct the file path on disk.
  112. //
  113. // This is mainly so you can push the generated code file to a shared
  114. // repository.
  115. Dev bool
  116. // When true, size, mode and modtime are not preserved from files
  117. NoMetadata bool
  118. // When nonzero, use this as mode for all files.
  119. Mode uint
  120. // When nonzero, use this as unix timestamp for all files.
  121. ModTime int64
  122. // Ignores any filenames matching the regex pattern specified, e.g.
  123. // path/to/file.ext will ignore only that file, or \\.gitignore
  124. // will match any .gitignore file.
  125. //
  126. // This parameter can be provided multiple times.
  127. Ignore []*regexp.Regexp
  128. }
  129. // NewConfig returns a default configuration struct.
  130. func NewConfig() *Config {
  131. c := new(Config)
  132. c.Package = "main"
  133. c.NoMemCopy = false
  134. c.NoCompress = false
  135. c.Debug = false
  136. c.Output = "./bindata.go"
  137. c.Ignore = make([]*regexp.Regexp, 0)
  138. return c
  139. }
  140. // validate ensures the config has sane values.
  141. // Part of which means checking if certain file/directory paths exist.
  142. func (c *Config) validate() error {
  143. if len(c.Package) == 0 {
  144. return fmt.Errorf("Missing package name")
  145. }
  146. for _, input := range c.Input {
  147. _, err := os.Lstat(input.Path)
  148. if err != nil {
  149. return fmt.Errorf("Failed to stat input path '%s': %v", input.Path, err)
  150. }
  151. }
  152. if len(c.Output) == 0 {
  153. cwd, err := os.Getwd()
  154. if err != nil {
  155. return fmt.Errorf("Unable to determine current working directory.")
  156. }
  157. c.Output = filepath.Join(cwd, "bindata.go")
  158. }
  159. stat, err := os.Lstat(c.Output)
  160. if err != nil {
  161. if !os.IsNotExist(err) {
  162. return fmt.Errorf("Output path: %v", err)
  163. }
  164. // File does not exist. This is fine, just make
  165. // sure the directory it is to be in exists.
  166. dir, _ := filepath.Split(c.Output)
  167. if dir != "" {
  168. err = os.MkdirAll(dir, 0744)
  169. if err != nil {
  170. return fmt.Errorf("Create output directory: %v", err)
  171. }
  172. }
  173. }
  174. if stat != nil && stat.IsDir() {
  175. return fmt.Errorf("Output path is a directory.")
  176. }
  177. return nil
  178. }