Fix generate doutput code. os.Error -> error. Remove calling of go fmt from code. This should really be done manually.

This commit is contained in:
jim teeuwen 2012-02-11 16:49:43 +01:00
parent 2b9a31984c
commit 02da4740e4
4 changed files with 1852 additions and 1878 deletions

View File

@ -1,23 +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.
If gofmt is available on the system, bindata will invoke it to format the
generated go file.
================================================================================
DEPENDENCIES
================================================================================
n/a
================================================================================
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.

View File

@ -8,35 +8,24 @@ import (
"compress/gzip"
"fmt"
"io"
"os"
"os/exec"
"path"
)
// If gofmt exists on the system, run it over the target file to
// fix up the generated code. This is not necessary, just a convenience.
func gofmt(file string) (err error) {
var prog string
if prog = os.Getenv("GOBIN"); len(prog) == 0 {
return
}
prog = path.Join(prog, "gofmt")
cmd := exec.Command(prog, "-w", file)
return cmd.Run()
}
// Translate the input file.
// input -> gzip -> gowriter -> output.
func translate(input io.Reader, output io.Writer, pkgname, funcname string) (err error) {
var gz *gzip.Compressor
fmt.Fprintf(output, `package %s
import ( "io"; "os"; "bytes"; "compress/gzip" )
func %s() ([]byte, os.Error) {
import (
"bytes"
"compress/gzip"
"io"
)
func %s() ([]byte, error) {
var gz *gzip.Decompressor
var err os.Error
var err error
if gz, err = gzip.NewReader(bytes.NewBuffer([]byte{`, pkgname, funcname)
if gz, err = gzip.NewWriter(&GoWriter{Writer: output}); err != nil {
@ -54,6 +43,8 @@ if gz, err = gzip.NewReader(bytes.NewBuffer([]byte{`, pkgname, funcname)
var b bytes.Buffer
io.Copy(&b, gz)
gz.Close()
return b.Bytes(), nil}`)
return b.Bytes(), nil
}`)
return
}

11
main.go
View File

@ -16,7 +16,7 @@ import (
const (
APP_NAME = "bindata"
APP_VERSION = "0.4"
APP_VERSION = "0.5"
)
func main() {
@ -89,9 +89,10 @@ func main() {
}
}
var err error
// Read the input file, transform it into a gzip compressed data stream and
// write it out as a go source file.
var err error
if pipe {
if err = translate(os.Stdin, os.Stdout, *pkgname, *funcname); err != nil {
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
@ -118,12 +119,6 @@ func main() {
return
}
// If gofmt exists on the system, use it to format the generated source file.
if err = gofmt(*out); err != nil {
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
return
}
fmt.Fprintln(os.Stdout, "[i] Done.")
}
}

View File

@ -10,7 +10,7 @@ func gophercolor_png() ([]byte, error) {
var gz *gzip.Decompressor
var err error
if gz, err = gzip.NewReader(bytes.NewBuffer([]byte{
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x34, 0x9b,
0x1f,0x8b,0x08,0x00,0x00,0x09,0x6e,0x88,0x00,0xff,0x34,0x9b,
0x65,0x50,0x1b,0x5f,0x17,0xc6,0x43,0x69,0x8b,0xbb,0xbb,0xbb,
0x16,0x2d,0x5e,0xb4,0xb8,0xbb,0x16,0x77,0x77,0xa7,0xb8,0x14,
0x77,0x28,0xee,0xee,0x4e,0x70,0x77,0x08,0x52,0xdc,0x09,0xee,
@ -1843,5 +1843,6 @@ func gophercolor_png() ([]byte, error) {
var b bytes.Buffer
io.Copy(&b, gz)
gz.Close()
return b.Bytes(), nil
}