Added facilities to pipe file data into stdin.
This commit is contained in:
parent
c539ce1afc
commit
55b35257df
8
README
8
README
|
@ -51,8 +51,14 @@ generated go file.
|
|||
|
||||
You can now simply include the new .go file in your program and call
|
||||
gophercolor_png() to get the uncompressed image data.
|
||||
|
||||
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.
|
||||
|
||||
|
|
27
bindata.go
27
bindata.go
|
@ -28,40 +28,25 @@ func gofmt(file string) (err os.Error) {
|
|||
|
||||
// Translate the input file.
|
||||
// input -> gzip -> gowriter -> output.
|
||||
func translate(input, output, pkgname, funcname string) (err os.Error) {
|
||||
var fs, fd *os.File
|
||||
func translate(input io.Reader, output io.Writer, pkgname, funcname string) (err os.Error) {
|
||||
var gz *gzip.Compressor
|
||||
|
||||
if fs, err = os.Open(input); err != nil {
|
||||
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
|
||||
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{`, 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
|
||||
}
|
||||
|
||||
io.Copy(gz, fs)
|
||||
io.Copy(gz, input)
|
||||
gz.Close()
|
||||
|
||||
fmt.Fprint(fd, `
|
||||
fmt.Fprint(output, `
|
||||
})); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
75
main.go
75
main.go
|
@ -15,11 +15,11 @@ import (
|
|||
|
||||
const (
|
||||
APP_NAME = "bindata"
|
||||
APP_VERSION = "0.2"
|
||||
APP_VERSION = "0.3"
|
||||
)
|
||||
|
||||
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.")
|
||||
pkgname := flag.String("p", "", "Optional name of the package to generate.")
|
||||
funcname := flag.String("f", "", "Optional name of the function to generate.")
|
||||
|
@ -33,12 +33,9 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
if len(*in) == 0 {
|
||||
fmt.Fprintln(os.Stderr, "[e] No input file specified.")
|
||||
os.Exit(1)
|
||||
}
|
||||
pipe := len(*in) == 0
|
||||
|
||||
if len(*out) == 0 {
|
||||
if !pipe && len(*out) == 0 {
|
||||
// Ensure we create our own output filename that does not already exist.
|
||||
dir, file := path.Split(*in)
|
||||
|
||||
|
@ -67,27 +64,57 @@ func main() {
|
|||
}
|
||||
|
||||
if len(*funcname) == 0 {
|
||||
_, file := path.Split(*in)
|
||||
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
|
||||
if pipe {
|
||||
// Can't infer from input file name in this mode.
|
||||
fmt.Fprintln(os.Stderr, "[e] No function name specified.")
|
||||
os.Exit(1)
|
||||
} else {
|
||||
_, file := path.Split(*in)
|
||||
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
|
||||
// write it out as a go source file.
|
||||
if err := translate(*in, *out, *pkgname, *funcname); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
||||
return
|
||||
}
|
||||
var err os.Error
|
||||
if pipe {
|
||||
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 err := gofmt(*out); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if fs, err = os.Open(*in); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintln(os.Stdout, "[i] Done.")
|
||||
defer fs.Close()
|
||||
|
||||
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.")
|
||||
}
|
||||
}
|
||||
|
|
2
testdata/gophercolor.png.go
vendored
2
testdata/gophercolor.png.go
vendored
|
@ -1,5 +1,3 @@
|
|||
// auto generated from 'testdata/gophercolor.png'.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
|
Loading…
Reference in New Issue
Block a user