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

pull/4/head
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,36 +8,25 @@ 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) {
var gz *gzip.Decompressor
var err os.Error
if gz, err = gzip.NewReader(bytes.NewBuffer([]byte{`, pkgname, funcname)
import (
"bytes"
"compress/gzip"
"io"
)
func %s() ([]byte, error) {
var gz *gzip.Decompressor
var err error
if gz, err = gzip.NewReader(bytes.NewBuffer([]byte{`, pkgname, funcname)
if gz, err = gzip.NewWriter(&GoWriter{Writer: output}); err != nil {
return
@ -47,13 +36,15 @@ if gz, err = gzip.NewReader(bytes.NewBuffer([]byte{`, pkgname, funcname)
gz.Close()
fmt.Fprint(output, `
})); err != nil {
return nil, err
}
})); err != nil {
return nil, err
}
var b bytes.Buffer
io.Copy(&b, gz)
gz.Close()
return b.Bytes(), nil}`)
var b bytes.Buffer
io.Copy(&b, gz)
gz.Close()
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.")
}
}

File diff suppressed because it is too large Load Diff