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.

pull/4/head
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,10 +1,10 @@
## 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.
@ -55,3 +55,16 @@ converted to a raw byte slice.
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