* 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
84
README.md
84
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 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 package name specified. Using 'main'.
|
||||
[w] No function name specified. Using 'gophercolor_png'.
|
||||
[i] Done.
|
||||
[w] No function name specified. Using 'testdata_gophercolor_png'.
|
||||
|
||||
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:
|
||||
|
||||
func gophercolor_png() []byte
|
||||
```go
|
||||
func testdata_gophercolor_png() []byte
|
||||
```
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
the compiled program's `.rodata` section. This ensures that when we call
|
||||
call our generated function, we omit unnecessary memcopies.
|
||||
|
@ -61,14 +69,17 @@ For instance, consider the following two examples:
|
|||
This would be the default mode, using an extra memcopy but gives a safe
|
||||
implementation without dependencies on `reflect` and `unsafe`:
|
||||
|
||||
```go
|
||||
func myfile() []byte {
|
||||
return []byte{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a}
|
||||
}
|
||||
```
|
||||
|
||||
Here is the same functionality, but uses the `.rodata` hack.
|
||||
The byte slice returned from this example can not be written to without
|
||||
generating a runtime error.
|
||||
|
||||
```go
|
||||
var _myfile = "\x89\x50\x4e\x47\x0d\x0a\x1a"
|
||||
|
||||
func myfile() []byte {
|
||||
|
@ -81,11 +92,12 @@ generating a runtime error.
|
|||
bx.Cap = bx.Len
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### 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
|
||||
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.
|
||||
|
||||
|
||||
### 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)
|
||||
```
|
||||
|
|
109
main.go
109
main.go
|
@ -9,25 +9,24 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
const (
|
||||
AppName = "bindata"
|
||||
AppVersion = "1.0.1"
|
||||
)
|
||||
|
||||
var (
|
||||
pipe = false
|
||||
in = flag.String("i", "", "Path to the input file. Alternatively, pipe the file data into stdin.")
|
||||
out = flag.String("o", "", "Optional path to the output file.")
|
||||
pkgname = flag.String("p", "", "Optional name of the package to generate.")
|
||||
funcname = flag.String("f", "", "Optional name of the function/variable 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.")
|
||||
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.")
|
||||
version = flag.Bool("v", false, "Display version information.")
|
||||
in = ""
|
||||
out = flag.String("out", "", "Optional path to the output file.")
|
||||
pkgname = flag.String("pkg", "main", "Name of the package to generate.")
|
||||
funcname = flag.String("func", "", "Optional name of the function to generate.")
|
||||
prefix = flag.String("prefix", "", "Optional path prefix to strip off map keys and function names.")
|
||||
uncompressed = flag.Bool("uncompressed", false, "The specified resource will /not/ be GZIP compressed when this flag is specified. This alters the generated output code.")
|
||||
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() {
|
||||
|
@ -35,8 +34,10 @@ func main() {
|
|||
|
||||
if pipe {
|
||||
translate(os.Stdin, os.Stdout, *pkgname, *funcname, *uncompressed, *nomemcopy)
|
||||
} else {
|
||||
fs, err := os.Open(*in)
|
||||
return
|
||||
}
|
||||
|
||||
fs, err := os.Open(in)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
||||
return
|
||||
|
@ -52,36 +53,58 @@ func main() {
|
|||
|
||||
defer fd.Close()
|
||||
|
||||
// Translate binary to Go code.
|
||||
translate(fs, fd, *pkgname, *funcname, *uncompressed, *nomemcopy)
|
||||
|
||||
fmt.Fprintln(os.Stdout, "[i] Done.")
|
||||
// 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 {
|
||||
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
writeTOCInit(fd, in, *prefix, *funcname)
|
||||
}
|
||||
}
|
||||
|
||||
// parseArgs processes and verifies commandline arguments.
|
||||
func parseArgs() {
|
||||
flag.Usage = func() {
|
||||
fmt.Printf("Usage: %s [options] <filename>\n\n", os.Args[0])
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
flag.Parse()
|
||||
|
||||
if *version {
|
||||
fmt.Fprintf(os.Stdout, "%s v%s (Go runtime %s)\n",
|
||||
AppName, AppVersion, runtime.Version())
|
||||
fmt.Printf("%s\n", Version())
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
pipe = len(*in) == 0
|
||||
pipe = flag.NArg() == 0
|
||||
|
||||
if !pipe && len(*out) == 0 {
|
||||
// Ensure we create our own output filename that does not already exist.
|
||||
dir, file := path.Split(*in)
|
||||
*prefix, _ = filepath.Abs(filepath.Clean(*prefix))
|
||||
in, _ = filepath.Abs(filepath.Clean(flag.Args()[0]))
|
||||
|
||||
*out = path.Join(dir, file) + ".go"
|
||||
if _, err := os.Lstat(*out); err == nil {
|
||||
// Ensure we create our own output filename that does not already exist.
|
||||
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
|
||||
// find a name that is available.
|
||||
count := 0
|
||||
|
||||
for {
|
||||
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
|
||||
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 {
|
||||
|
@ -110,18 +133,36 @@ func parseArgs() {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
_, file := path.Split(*in)
|
||||
file = strings.ToLower(file)
|
||||
file = strings.Replace(file, " ", "_", -1)
|
||||
file = strings.Replace(file, ".", "_", -1)
|
||||
file = strings.Replace(file, "-", "_", -1)
|
||||
*funcname = safeFuncname(in, *prefix)
|
||||
fmt.Fprintf(os.Stderr, "[w] No function name specified. Using %s.\n", *funcname)
|
||||
}
|
||||
}
|
||||
|
||||
if unicode.IsDigit(rune(file[0])) {
|
||||
// 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.
|
||||
file = "_" + file
|
||||
name = "_" + name
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "[w] No function name specified. Using '%s'.\n", file)
|
||||
*funcname = file
|
||||
// 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