Added -u flag. When this is supplied to the program, no compression of the input data will take place. This also means the generated source code changes from a function to a global variable. The function is only there because it has to decompress the binary data. When no compression is used, this is obviously not necessary. Default behaviour is still the same (with compression) so existing code using this program will not break. Fixed README to reflect new behaviour. Added new testdata examples for both compressed and uncompressed output.

This commit is contained in:
jim teeuwen 2012-03-28 01:39:01 +02:00
parent 0450ba8421
commit 384e0cafe9
6 changed files with 3741 additions and 1877 deletions

View File

@ -1,13 +1,13 @@
## Bindata
## bindata
This tool converts any file into managable Go source code. Useful for embedding
binary data into a go program. The file data is gzip compressed before being
converted to a raw byte slice.
### USAGE
### Usage
The simplest invocation is to pass it only the input file name.
The output file and code settings are inferred from this automatically.
The simplest invocation is to pass it only the input file name.
The output file and code settings are inferred from this automatically.
$ bindata -i testdata/gophercolor.png
[w] No output file specified. Using 'testdata/gophercolor.png.go'.
@ -15,9 +15,9 @@ converted to a raw byte slice.
[w] No function name specified. Using 'gophercolor_png'.
[i] Done.
This creates the "testdata/gophercolor.png.go" file which has a package
declaration with name 'main' and one function named 'gophercolor_png'.
It looks like this:
This creates the "testdata/gophercolor.png.go" file which has a package
declaration with name 'main' and one function named 'gophercolor_png'.
It looks like this:
// gophercolor_png returns the decompressed binary data.
// It panics if an error occurred.
@ -37,21 +37,34 @@ converted to a raw byte slice.
return b.Bytes()
}
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
if something went wrong during decompression. This makes any faults appearant
during initialization of your program, so it can quickly be fixed. Additionally,
this approach allows us to assign the decompressed file data to global
variables where necessary.
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
if something went wrong during decompression. This makes any faults appearant
during initialization of your program, so it can quickly be fixed. Additionally,
this approach allows us to assign the decompressed file data to global
variables where necessary.
See the testdata directory for example input and output.
See the testdata directory for example input and output.
Aternatively, you can pipe the input file data into stdin. bindata will then
spit out the generated Go code to stdout. This does require explicitly naming
the desired function name, as it can not be inferred from the input data.
The package name will still default to 'main'.
Aternatively, you can pipe the input file data into stdin. bindata will then
spit out the generated Go code to stdout. This does require explicitly naming
the desired function name, as it can not be inferred from the input data.
The package name will still default to 'main'.
$ cat testdata/gophercolor.png | ./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.
### Optional compression
When the `-u` flag is given, the supplied resource is *not* GZIP compressed
before being turned into Go code. This also alters the generated output in that
we no longer need a function that decompresses the data. The resource's raw
byte data is simply assigned to a global variable of the same name as the
function would otherwise get. This feature is useful if you do not care for
compression, or the supplied resource is already compressed. Doing it again
would not add any value and may even increase the size of the data.
The default behaviour of the program is to use compression.

View File

@ -10,9 +10,18 @@ import (
"io"
)
// Translate the input file.
// Translate the input file without GZIP compression.
// input -> gowriter -> output.
func translate_uncompressed(input io.Reader, output io.Writer, pkgname, funcname string) (err error) {
fmt.Fprintf(output, "package %s\n\nvar %s []byte = []byte{", pkgname, funcname)
io.Copy(&GoWriter{Writer: output}, input)
fmt.Fprint(output, "\n}")
return
}
// Translate the input file with GZIP compression.
// input -> gzip -> gowriter -> output.
func translate(input io.Reader, output io.Writer, pkgname, funcname string) (err error) {
func translate_compressed(input io.Reader, output io.Writer, pkgname, funcname string) (err error) {
fmt.Fprintf(output, `package %s
import (

23
main.go
View File

@ -15,8 +15,8 @@ import (
)
const (
APP_NAME = "bindata"
APP_VERSION = "0.5"
AppName = "bindata"
AppVersion = "0.6"
)
func main() {
@ -24,13 +24,14 @@ func main() {
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 to generate.")
uncompressed := flag.Bool("u", false, "The specified resource will /not/ be GZIP compressed when this flag isspecified. This alters the generated output code.")
version := flag.Bool("v", false, "Display version information.")
flag.Parse()
if *version {
fmt.Fprintf(os.Stdout, "%s v%s (Go runtime %s)\n",
APP_NAME, APP_VERSION, runtime.Version())
AppName, AppVersion, runtime.Version())
return
}
@ -94,7 +95,13 @@ func main() {
// Read the input file, transform it into a gzip compressed data stream and
// write it out as a go source file.
if pipe {
if err = translate(os.Stdin, os.Stdout, *pkgname, *funcname); err != nil {
if *uncompressed {
err = translate_uncompressed(os.Stdin, os.Stdout, *pkgname, *funcname)
} else {
err = translate_compressed(os.Stdin, os.Stdout, *pkgname, *funcname)
}
if err != nil {
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
}
} else {
@ -114,7 +121,13 @@ func main() {
defer fd.Close()
if err = translate(fs, fd, *pkgname, *funcname); err != nil {
if *uncompressed {
err = translate_uncompressed(fs, fd, *pkgname, *funcname)
} else {
err = translate_compressed(fs, fd, *pkgname, *funcname)
}
if err != nil {
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
return
}

1850
testdata/gophercolor.png-compressed.go vendored Normal file

File diff suppressed because it is too large Load Diff

1829
testdata/gophercolor.png-uncompressed.go vendored Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff