* Bump version to new release: 2.0.0
We do this, because the changes in this patch fundamentally alter the way code is generated by the tool. This will, in some cases, be incompatible with older versions. * Performs cleanup and minor code fixes. * Adds two new command line flags: * -prefix: This accepts a partial path. It is used when generating a target function name, as well as the key for the Table of Contents map (see below). The specified prefix is applied to the input file name, causing the prefix section to be stripped from the input file path. E.g.: ``` input: /path/to/foo.x prefix: /path/to output: /foo.x ``` * -toc: This is a boolean flag which tells the tool to generate a table of contents for the generated data files. It creates a separate 'bindata-toc.go' file, which defines a global map named `go_bindata`. It then appends an `init` function to the generated file. This function makes the data function register itself with the global map. * Fixes the function names the tool infers from input file names to include the full path. This fixes potential name collisions when the same file name is processed from different directories. For example, we can now safely import the following two files: ``` input file: css/ie/foo.css output function: css_ie_foo_css() input file: css/chrome/foo.css output function: css_chrome_foo_css() ```
This commit is contained in:
parent
6a3f0aca94
commit
ff063d28c6
112
README.md
112
README.md
|
@ -9,20 +9,21 @@ before being converted to a raw byte slice.
|
||||||
The simplest invocation is to pass it only the input file name.
|
The simplest invocation is to pass it only the input file name.
|
||||||
The output file and code settings are inferred from this automatically.
|
The output file and code settings are inferred from this automatically.
|
||||||
|
|
||||||
$ go-bindata -i testdata/gophercolor.png
|
$ go-bindata testdata/gophercolor.png
|
||||||
[w] No output file specified. Using 'testdata/gophercolor.png.go'.
|
[w] No output file specified. Using 'testdata/gophercolor.png.go'.
|
||||||
[w] No package name specified. Using 'main'.
|
[w] No package name specified. Using 'main'.
|
||||||
[w] No function name specified. Using 'gophercolor_png'.
|
[w] No function name specified. Using 'testdata_gophercolor_png'.
|
||||||
[i] Done.
|
|
||||||
|
|
||||||
This creates the `testdata/gophercolor.png.go` file which has a package
|
This creates the `testdata/gophercolor.png.go` file which has a package
|
||||||
declaration with name `main` and one function named `gophercolor_png` with
|
declaration with name `main` and one function named `testdata_gophercolor_png` with
|
||||||
the following signature:
|
the following signature:
|
||||||
|
|
||||||
func gophercolor_png() []byte
|
```go
|
||||||
|
func testdata_gophercolor_png() []byte
|
||||||
|
```
|
||||||
|
|
||||||
You can now simply include the new .go file in your program and call
|
You can now simply include the new .go file in your program and call
|
||||||
`gophercolor_png()` to get the (uncompressed) image data. The function panics
|
`testdata_gophercolor_png()` to get the (uncompressed) image data. The function panics
|
||||||
if something went wrong during decompression. See the testdata directory for
|
if something went wrong during decompression. See the testdata directory for
|
||||||
example input and output files for various modes.
|
example input and output files for various modes.
|
||||||
|
|
||||||
|
@ -33,12 +34,19 @@ input data. The package name will still default to 'main'.
|
||||||
|
|
||||||
$ cat testdata/gophercolor.png | go-bindata -f gophercolor_png | gofmt
|
$ cat testdata/gophercolor.png | go-bindata -f gophercolor_png | gofmt
|
||||||
|
|
||||||
Invoke the program with the -h flag for more options.
|
Invoke the program with the `-h` flag for more options.
|
||||||
|
|
||||||
|
In order to strip off a part of the generated function name, we can use the `-prefix` flag.
|
||||||
|
In the above example, the input file `testdata/gophercolor.png` yields a function named
|
||||||
|
`testdata_gophercolor_png`. If we want the `testdata` component to be left out, we invoke
|
||||||
|
the program as follows:
|
||||||
|
|
||||||
|
$ go-bindata -prefix "testdata/" testdata/gophercolor.png
|
||||||
|
|
||||||
|
|
||||||
### Lower memory footprint
|
### Lower memory footprint
|
||||||
|
|
||||||
Using the `-m` flag, will alter the way the output file is generated.
|
Using the `-nomemcopy` flag, will alter the way the output file is generated.
|
||||||
It will employ a hack that allows us to read the file data directly from
|
It will employ a hack that allows us to read the file data directly from
|
||||||
the compiled program's `.rodata` section. This ensures that when we call
|
the compiled program's `.rodata` section. This ensures that when we call
|
||||||
call our generated function, we omit unnecessary memcopies.
|
call our generated function, we omit unnecessary memcopies.
|
||||||
|
@ -61,31 +69,35 @@ For instance, consider the following two examples:
|
||||||
This would be the default mode, using an extra memcopy but gives a safe
|
This would be the default mode, using an extra memcopy but gives a safe
|
||||||
implementation without dependencies on `reflect` and `unsafe`:
|
implementation without dependencies on `reflect` and `unsafe`:
|
||||||
|
|
||||||
func myfile() []byte {
|
```go
|
||||||
return []byte{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a}
|
func myfile() []byte {
|
||||||
}
|
return []byte{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Here is the same functionality, but uses the `.rodata` hack.
|
Here is the same functionality, but uses the `.rodata` hack.
|
||||||
The byte slice returned from this example can not be written to without
|
The byte slice returned from this example can not be written to without
|
||||||
generating a runtime error.
|
generating a runtime error.
|
||||||
|
|
||||||
var _myfile = "\x89\x50\x4e\x47\x0d\x0a\x1a"
|
```go
|
||||||
|
var _myfile = "\x89\x50\x4e\x47\x0d\x0a\x1a"
|
||||||
|
|
||||||
func myfile() []byte {
|
func myfile() []byte {
|
||||||
var empty [0]byte
|
var empty [0]byte
|
||||||
sx := (*reflect.StringHeader)(unsafe.Pointer(&_myfile))
|
sx := (*reflect.StringHeader)(unsafe.Pointer(&_myfile))
|
||||||
b := empty[:]
|
b := empty[:]
|
||||||
bx := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
bx := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
||||||
bx.Data = sx.Data
|
bx.Data = sx.Data
|
||||||
bx.Len = len(_myfile)
|
bx.Len = len(_myfile)
|
||||||
bx.Cap = bx.Len
|
bx.Cap = bx.Len
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Optional compression
|
### Optional compression
|
||||||
|
|
||||||
When the `-u` flag is given, the supplied resource is *not* GZIP compressed
|
When the `-uncompressed` flag is given, the supplied resource is *not* GZIP compressed
|
||||||
before being turned into Go code. The data should still be accessed through
|
before being turned into Go code. The data should still be accessed through
|
||||||
a function call, so nothing changes in the usage of the generated file.
|
a function call, so nothing changes in the usage of the generated file.
|
||||||
|
|
||||||
|
@ -95,3 +107,57 @@ even increase the size of the data.
|
||||||
|
|
||||||
The default behaviour of the program is to use compression.
|
The default behaviour of the program is to use compression.
|
||||||
|
|
||||||
|
|
||||||
|
### Table of Contents
|
||||||
|
|
||||||
|
With the `-toc` flag, we can have `go-bindata` create a table of contents for all the files
|
||||||
|
which have been generated by the tool. It does this by first generating a new file named
|
||||||
|
`bindata-toc.go`. This contains a global map of type `map[string] func() []byte`. It uses the
|
||||||
|
input filename as the key and the data function as the value. We can use this
|
||||||
|
to fetch all data for our files, matching a given pattern.
|
||||||
|
|
||||||
|
It then appands an `init` function to each generated file, which simply makes the data
|
||||||
|
function append itself to the global `bindata` map.
|
||||||
|
|
||||||
|
Once you have compiled your program with all these new files and run it, the map will
|
||||||
|
be populated by all generated data files.
|
||||||
|
|
||||||
|
**Note**: The `bindata-toc.go` file will not be created when we run in `pipe` mode.
|
||||||
|
The reason being, that the tool does not write any files at all, as it has no idea
|
||||||
|
where to save them. The data file is written to `stdout` instead after all.
|
||||||
|
|
||||||
|
|
||||||
|
#### Table of Contents keys
|
||||||
|
|
||||||
|
The keys used in the `go_bindata` map, are the same as the input file name passed to `go-bindata`.
|
||||||
|
This includes the fully qualified (absolute) path. In most cases, this is not desireable, as it
|
||||||
|
puts potentially sensitive information in your code base. For this purpose, the tool supplies
|
||||||
|
another command line flag `-prefix`. This accepts a portion of a path name, which should be
|
||||||
|
stripped off from the map keys and function names.
|
||||||
|
|
||||||
|
For example, running without the `-prefix` flag, we get:
|
||||||
|
|
||||||
|
$ go-bindata /path/to/templates/foo.html
|
||||||
|
|
||||||
|
go_bindata["/path/to/templates/foo.html"] = path_to_templates_foo_html
|
||||||
|
|
||||||
|
Running with the `-prefix` flag, we get:
|
||||||
|
|
||||||
|
$ go-bindata -prefix "/path/to/" /path/to/templates/foo.html
|
||||||
|
|
||||||
|
go_bindata["templates/foo.html"] = templates_foo_html
|
||||||
|
|
||||||
|
|
||||||
|
#### bindata-toc.go
|
||||||
|
|
||||||
|
The `bindata-toc.go` file is very simple and looks as follows:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package $PACKAGENAME
|
||||||
|
|
||||||
|
// Global Table of Contents map. Generated by go-bindata.
|
||||||
|
// After startup of the program, all generated data files will
|
||||||
|
// put themselves in this map. The key is the full filename, as
|
||||||
|
// supplied to go-bindata.
|
||||||
|
var go_bindata = make(map[string] func() []byte)
|
||||||
|
```
|
||||||
|
|
141
main.go
141
main.go
|
@ -9,25 +9,24 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"runtime"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
AppName = "bindata"
|
|
||||||
AppVersion = "1.0.1"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
pipe = false
|
pipe = false
|
||||||
in = flag.String("i", "", "Path to the input file. Alternatively, pipe the file data into stdin.")
|
in = ""
|
||||||
out = flag.String("o", "", "Optional path to the output file.")
|
out = flag.String("out", "", "Optional path to the output file.")
|
||||||
pkgname = flag.String("p", "", "Optional name of the package to generate.")
|
pkgname = flag.String("pkg", "main", "Name of the package to generate.")
|
||||||
funcname = flag.String("f", "", "Optional name of the function/variable to generate.")
|
funcname = flag.String("func", "", "Optional name of the function to generate.")
|
||||||
uncompressed = flag.Bool("u", false, "The specified resource will /not/ be GZIP compressed when this flag is specified. This alters the generated output code.")
|
prefix = flag.String("prefix", "", "Optional path prefix to strip off map keys and function names.")
|
||||||
nomemcopy = flag.Bool("m", false, "Use the memcopy hack to get rid of unnecessary memcopies. Refer to the documentation to see what implications this carries.")
|
uncompressed = flag.Bool("uncompressed", false, "The specified resource will /not/ be GZIP compressed when this flag is specified. This alters the generated output code.")
|
||||||
version = flag.Bool("v", false, "Display version information.")
|
nomemcopy = flag.Bool("nomemcopy", false, "Use a .rodata hack to get rid of unnecessary memcopies. Refer to the documentation to see what implications this carries.")
|
||||||
|
toc = flag.Bool("toc", false, "Generate a table of contents for this and other files. The input filepath becomes the map key. This option is only useable in non-pipe mode.")
|
||||||
|
version = flag.Bool("version", false, "Display version information.")
|
||||||
|
regFuncName = regexp.MustCompile(`[^a-zA-Z0-9_]`)
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -35,53 +34,77 @@ func main() {
|
||||||
|
|
||||||
if pipe {
|
if pipe {
|
||||||
translate(os.Stdin, os.Stdout, *pkgname, *funcname, *uncompressed, *nomemcopy)
|
translate(os.Stdin, os.Stdout, *pkgname, *funcname, *uncompressed, *nomemcopy)
|
||||||
} else {
|
return
|
||||||
fs, err := os.Open(*in)
|
}
|
||||||
|
|
||||||
|
fs, err := os.Open(in)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer fs.Close()
|
||||||
|
|
||||||
|
fd, err := os.Create(*out)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer fd.Close()
|
||||||
|
|
||||||
|
// Translate binary to Go code.
|
||||||
|
translate(fs, fd, *pkgname, *funcname, *uncompressed, *nomemcopy)
|
||||||
|
|
||||||
|
// Append the TOC init function to the end of the output file and
|
||||||
|
// write the `bindata-toc.go` file, if applicable.
|
||||||
|
if *toc {
|
||||||
|
err := createTOC(in, *pkgname)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer fs.Close()
|
writeTOCInit(fd, in, *prefix, *funcname)
|
||||||
|
|
||||||
fd, err := os.Create(*out)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer fd.Close()
|
|
||||||
|
|
||||||
translate(fs, fd, *pkgname, *funcname, *uncompressed, *nomemcopy)
|
|
||||||
|
|
||||||
fmt.Fprintln(os.Stdout, "[i] Done.")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseArgs processes and verifies commandline arguments.
|
// parseArgs processes and verifies commandline arguments.
|
||||||
func parseArgs() {
|
func parseArgs() {
|
||||||
|
flag.Usage = func() {
|
||||||
|
fmt.Printf("Usage: %s [options] <filename>\n\n", os.Args[0])
|
||||||
|
flag.PrintDefaults()
|
||||||
|
}
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if *version {
|
if *version {
|
||||||
fmt.Fprintf(os.Stdout, "%s v%s (Go runtime %s)\n",
|
fmt.Printf("%s\n", Version())
|
||||||
AppName, AppVersion, runtime.Version())
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pipe = len(*in) == 0
|
pipe = flag.NArg() == 0
|
||||||
|
|
||||||
if !pipe && len(*out) == 0 {
|
if !pipe && len(*out) == 0 {
|
||||||
// Ensure we create our own output filename that does not already exist.
|
*prefix, _ = filepath.Abs(filepath.Clean(*prefix))
|
||||||
dir, file := path.Split(*in)
|
in, _ = filepath.Abs(filepath.Clean(flag.Args()[0]))
|
||||||
|
|
||||||
*out = path.Join(dir, file) + ".go"
|
// Ensure we create our own output filename that does not already exist.
|
||||||
if _, err := os.Lstat(*out); err == nil {
|
dir, file := path.Split(in)
|
||||||
|
|
||||||
|
*out = path.Join(dir, file+".go")
|
||||||
|
_, err := os.Lstat(*out)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
// File already exists. Pad name with a sequential number until we
|
// File already exists. Pad name with a sequential number until we
|
||||||
// find a name that is available.
|
// find a name that is available.
|
||||||
count := 0
|
count := 0
|
||||||
|
|
||||||
for {
|
for {
|
||||||
f := path.Join(dir, fmt.Sprintf("%s.%d.go", file, count))
|
f := path.Join(dir, fmt.Sprintf("%s.%d.go", file, count))
|
||||||
if _, err := os.Lstat(f); err != nil {
|
_, err = os.Lstat(f)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
*out = f
|
*out = f
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -90,7 +113,7 @@ func parseArgs() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "[w] No output file specified. Using '%s'.\n", *out)
|
fmt.Fprintf(os.Stderr, "[w] No output file specified. Using %s.\n", *out)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(*pkgname) == 0 {
|
if len(*pkgname) == 0 {
|
||||||
|
@ -110,18 +133,36 @@ func parseArgs() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, file := path.Split(*in)
|
*funcname = safeFuncname(in, *prefix)
|
||||||
file = strings.ToLower(file)
|
fmt.Fprintf(os.Stderr, "[w] No function name specified. Using %s.\n", *funcname)
|
||||||
file = strings.Replace(file, " ", "_", -1)
|
|
||||||
file = strings.Replace(file, ".", "_", -1)
|
|
||||||
file = strings.Replace(file, "-", "_", -1)
|
|
||||||
|
|
||||||
if unicode.IsDigit(rune(file[0])) {
|
|
||||||
// Identifier can't start with a digit.
|
|
||||||
file = "_" + file
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "[w] No function name specified. Using '%s'.\n", file)
|
|
||||||
*funcname = file
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// safeFuncname creates a safe function name from the input path.
|
||||||
|
func safeFuncname(in, prefix string) string {
|
||||||
|
name := strings.Replace(in, prefix, "", 1)
|
||||||
|
|
||||||
|
if len(name) == 0 {
|
||||||
|
name = in
|
||||||
|
}
|
||||||
|
|
||||||
|
name = strings.ToLower(name)
|
||||||
|
name = regFuncName.ReplaceAllString(name, "_")
|
||||||
|
|
||||||
|
if unicode.IsDigit(rune(name[0])) {
|
||||||
|
// Identifier can't start with a digit.
|
||||||
|
name = "_" + name
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get rid of "__" instances for niceness.
|
||||||
|
for strings.Index(name, "__") > -1 {
|
||||||
|
name = strings.Replace(name, "__", "_", -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Leading underscore is silly.
|
||||||
|
if name[0] == '_' {
|
||||||
|
name = name[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
7
testdata/bindata-toc.go
vendored
Normal file
7
testdata/bindata-toc.go
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
// Global Table of Contents map. Generated by go-bindata.
|
||||||
|
// After startup of the program, all generated data files will
|
||||||
|
// put themselves in this map. The key is the full filename, as
|
||||||
|
// supplied to go-bindata.
|
||||||
|
var go_bindata = make(map[string]func() []byte)
|
1853
testdata/toc.go
vendored
Normal file
1853
testdata/toc.go
vendored
Normal file
File diff suppressed because it is too large
Load Diff
39
toc.go
Normal file
39
toc.go
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
|
||||||
|
// license. Its contents can be found at:
|
||||||
|
// http://creativecommons.org/publicdomain/zero/1.0/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// createTOC writes a table of contents file to the given location.
|
||||||
|
func createTOC(file, pkgname string) error {
|
||||||
|
dir, _ := filepath.Split(file)
|
||||||
|
file = filepath.Join(dir, "bindata-toc.go")
|
||||||
|
code := fmt.Sprintf(`package %s
|
||||||
|
|
||||||
|
// Global Table of Contents map. Generated by go-bindata.
|
||||||
|
// After startup of the program, all generated data files will
|
||||||
|
// put themselves in this map. The key is the full filename, as
|
||||||
|
// supplied to go-bindata.
|
||||||
|
var go_bindata = make(map[string] func() []byte)`, pkgname)
|
||||||
|
|
||||||
|
return ioutil.WriteFile(file, []byte(code), 0600)
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeTOCInit writes the TOC init function for a given data file.
|
||||||
|
func writeTOCInit(output io.Writer, filename, prefix, funcname string) {
|
||||||
|
filename = strings.Replace(filename, prefix, "", 1)
|
||||||
|
fmt.Fprintf(output, `
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
go_bindata[%q] = %s
|
||||||
|
}
|
||||||
|
`, filename, funcname)
|
||||||
|
}
|
31
version.go
Normal file
31
version.go
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
|
||||||
|
// license. Its contents can be found at:
|
||||||
|
// http://creativecommons.org/publicdomain/zero/1.0/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
AppName = "bindata"
|
||||||
|
AppVersionMajor = 2
|
||||||
|
AppVersionMinor = 0
|
||||||
|
)
|
||||||
|
|
||||||
|
// revision part of the program version.
|
||||||
|
// This will be set automatically at build time like so:
|
||||||
|
//
|
||||||
|
// go build -ldflags "-X main.AppVersionRev `date -u +%s`"
|
||||||
|
var AppVersionRev string
|
||||||
|
|
||||||
|
func Version() string {
|
||||||
|
if len(AppVersionRev) == 0 {
|
||||||
|
AppVersionRev = "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s %d.%d.%s (Go runtime %s).\nCopyright (c) 2010-2013, Jim Teeuwen.",
|
||||||
|
AppName, AppVersionMajor, AppVersionMinor, AppVersionRev, runtime.Version())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user