Added facilities to pipe file data into stdin.

pull/4/head
jim teeuwen 2011-06-17 18:52:40 +02:00
parent c539ce1afc
commit 55b35257df
4 changed files with 64 additions and 48 deletions

8
README
View File

@ -51,8 +51,14 @@ generated go file.
You can now simply include the new .go file in your program and call You can now simply include the new .go file in your program and call
gophercolor_png() to get the uncompressed image data. gophercolor_png() to get the uncompressed image data.
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'.
$ 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.

View File

@ -28,40 +28,25 @@ func gofmt(file string) (err os.Error) {
// Translate the input file. // Translate the input file.
// input -> gzip -> gowriter -> output. // input -> gzip -> gowriter -> output.
func translate(input, output, pkgname, funcname string) (err os.Error) { func translate(input io.Reader, output io.Writer, pkgname, funcname string) (err os.Error) {
var fs, fd *os.File
var gz *gzip.Compressor var gz *gzip.Compressor
if fs, err = os.Open(input); err != nil { fmt.Fprintf(output, `package %s
return
}
defer fs.Close()
if fd, err = os.Create(output); err != nil {
return
}
defer fd.Close()
fmt.Fprintf(fd, `// auto generated from '%s'.
package %s
import ( "io"; "os"; "bytes"; "compress/gzip" ) import ( "io"; "os"; "bytes"; "compress/gzip" )
func %s() ([]byte, os.Error) { func %s() ([]byte, os.Error) {
var gz *gzip.Decompressor var gz *gzip.Decompressor
var err os.Error var err os.Error
if gz, err = gzip.NewReader(bytes.NewBuffer([]byte{`, input, pkgname, funcname) if gz, err = gzip.NewReader(bytes.NewBuffer([]byte{`, pkgname, funcname)
if gz, err = gzip.NewWriter(&GoWriter{Writer: fd}); err != nil { if gz, err = gzip.NewWriter(&GoWriter{Writer: output}); err != nil {
return return
} }
io.Copy(gz, fs) io.Copy(gz, input)
gz.Close() gz.Close()
fmt.Fprint(fd, ` fmt.Fprint(output, `
})); err != nil { })); err != nil {
return nil, err return nil, err
} }

75
main.go
View File

@ -15,11 +15,11 @@ import (
const ( const (
APP_NAME = "bindata" APP_NAME = "bindata"
APP_VERSION = "0.2" APP_VERSION = "0.3"
) )
func main() { func main() {
in := flag.String("i", "", "Path to the input file.") 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.") out := flag.String("o", "", "Optional path to the output file.")
pkgname := flag.String("p", "", "Optional name of the package to generate.") pkgname := flag.String("p", "", "Optional name of the package to generate.")
funcname := flag.String("f", "", "Optional name of the function to generate.") funcname := flag.String("f", "", "Optional name of the function to generate.")
@ -33,12 +33,9 @@ func main() {
return return
} }
if len(*in) == 0 { pipe := len(*in) == 0
fmt.Fprintln(os.Stderr, "[e] No input file specified.")
os.Exit(1)
}
if len(*out) == 0 { if !pipe && len(*out) == 0 {
// Ensure we create our own output filename that does not already exist. // Ensure we create our own output filename that does not already exist.
dir, file := path.Split(*in) dir, file := path.Split(*in)
@ -67,27 +64,57 @@ func main() {
} }
if len(*funcname) == 0 { if len(*funcname) == 0 {
_, file := path.Split(*in) if pipe {
file = strings.ToLower(file) // Can't infer from input file name in this mode.
file = strings.Replace(file, " ", "_", -1) fmt.Fprintln(os.Stderr, "[e] No function name specified.")
file = strings.Replace(file, ".", "_", -1) os.Exit(1)
file = strings.Replace(file, "-", "_", -1) } else {
fmt.Fprintf(os.Stderr, "[w] No function name specified. Using '%s'.\n", file) _, file := path.Split(*in)
*funcname = file file = strings.ToLower(file)
file = strings.Replace(file, " ", "_", -1)
file = strings.Replace(file, ".", "_", -1)
file = strings.Replace(file, "-", "_", -1)
fmt.Fprintf(os.Stderr, "[w] No function name specified. Using '%s'.\n", file)
*funcname = file
}
} }
// Read the input file, transform it into a gzip compressed data stream and // Read the input file, transform it into a gzip compressed data stream and
// write it out as a go source file. // write it out as a go source file.
if err := translate(*in, *out, *pkgname, *funcname); err != nil { var err os.Error
fmt.Fprintf(os.Stderr, "[e] %s\n", err) if pipe {
return if err = translate(os.Stdin, os.Stdout, *pkgname, *funcname); err != nil {
} fmt.Fprintf(os.Stderr, "[e] %s\n", err)
return
}
} else {
var fs, fd *os.File
// If gofmt exists on the system, use it to format the generated source file. if fs, err = os.Open(*in); err != nil {
if err := gofmt(*out); err != nil { fmt.Fprintf(os.Stderr, "[e] %s\n", err)
fmt.Fprintf(os.Stderr, "[e] %s\n", err) return
os.Exit(1) }
}
defer fs.Close()
fmt.Fprintln(os.Stdout, "[i] Done.") if fd, err = os.Create(*out); err != nil {
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
return
}
defer fd.Close()
if err = translate(fs, fd, *pkgname, *funcname); err != nil {
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
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

@ -1,5 +1,3 @@
// auto generated from 'testdata/gophercolor.png'.
package main package main
import ( import (