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
|
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.
|
||||||
|
|
||||||
|
|
27
bindata.go
27
bindata.go
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
47
main.go
47
main.go
|
@ -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,6 +64,11 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(*funcname) == 0 {
|
if len(*funcname) == 0 {
|
||||||
|
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 := path.Split(*in)
|
||||||
file = strings.ToLower(file)
|
file = strings.ToLower(file)
|
||||||
file = strings.Replace(file, " ", "_", -1)
|
file = strings.Replace(file, " ", "_", -1)
|
||||||
|
@ -75,19 +77,44 @@ func main() {
|
||||||
fmt.Fprintf(os.Stderr, "[w] No function name specified. Using '%s'.\n", file)
|
fmt.Fprintf(os.Stderr, "[w] No function name specified. Using '%s'.\n", file)
|
||||||
*funcname = 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
|
||||||
|
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 fs, err = os.Open(*in); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// If gofmt exists on the system, use it to format the generated source file.
|
// If gofmt exists on the system, use it to format the generated source file.
|
||||||
if err := gofmt(*out); err != nil {
|
if err = gofmt(*out); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
fmt.Fprintf(os.Stderr, "[e] %s\n", err)
|
||||||
os.Exit(1)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintln(os.Stdout, "[i] Done.")
|
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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
Loading…
Reference in New Issue
Block a user