2011-06-17 16:01:49 +00:00
// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// license. Its contents can be found at:
// http://creativecommons.org/publicdomain/zero/1.0/
2011-06-17 15:44:59 +00:00
package main
import (
"flag"
"fmt"
2011-12-07 12:49:06 +00:00
"os"
2011-06-17 15:44:59 +00:00
"path"
"runtime"
2011-12-07 12:49:06 +00:00
"strings"
2011-06-27 16:45:39 +00:00
"unicode"
2011-06-17 15:44:59 +00:00
)
const (
2012-03-27 23:39:01 +00:00
AppName = "bindata"
2012-06-13 12:17:25 +00:00
AppVersion = "0.8"
2011-06-17 15:44:59 +00:00
)
func main ( ) {
2011-06-17 16:52:40 +00:00
in := flag . String ( "i" , "" , "Path to the input file. Alternatively, pipe the file data into stdin." )
2011-06-17 15:44:59 +00:00
out := flag . String ( "o" , "" , "Optional path to the output file." )
pkgname := flag . String ( "p" , "" , "Optional name of the package to generate." )
2012-03-27 23:43:03 +00:00
funcname := flag . String ( "f" , "" , "Optional name of the function/variable to generate." )
uncompressed := flag . Bool ( "u" , false , "The specified resource will /not/ be GZIP compressed when this flag is specified. This alters the generated output code." )
2011-06-17 15:44:59 +00:00
version := flag . Bool ( "v" , false , "Display version information." )
flag . Parse ( )
if * version {
fmt . Fprintf ( os . Stdout , "%s v%s (Go runtime %s)\n" ,
2012-03-27 23:39:01 +00:00
AppName , AppVersion , runtime . Version ( ) )
2011-06-17 15:44:59 +00:00
return
}
2011-06-17 16:52:40 +00:00
pipe := len ( * in ) == 0
2011-06-17 15:44:59 +00:00
2011-06-17 16:52:40 +00:00
if ! pipe && len ( * out ) == 0 {
2011-06-17 15:44:59 +00:00
// Ensure we create our own output filename that does not already exist.
dir , file := path . Split ( * in )
* out = path . Join ( dir , file ) + ".go"
if _ , err := os . Lstat ( * out ) ; err == nil {
// File already exists. Pad name with a sequential number until we
// find a name that is available.
count := 0
for {
f := path . Join ( dir , fmt . Sprintf ( "%s.%d.go" , file , count ) )
if _ , err := os . Lstat ( f ) ; err != nil {
* out = f
break
}
count ++
}
}
fmt . Fprintf ( os . Stderr , "[w] No output file specified. Using '%s'.\n" , * out )
}
if len ( * pkgname ) == 0 {
fmt . Fprintln ( os . Stderr , "[w] No package name specified. Using 'main'." )
* pkgname = "main"
2011-06-27 16:45:39 +00:00
} else {
2011-10-26 09:59:30 +00:00
if unicode . IsDigit ( rune ( ( * pkgname ) [ 0 ] ) ) {
2011-06-27 16:45:39 +00:00
// Identifier can't start with a digit.
* pkgname = "_" + * pkgname
}
2011-06-17 15:44:59 +00:00
}
if len ( * funcname ) == 0 {
2011-06-17 16:52:40 +00:00
if pipe {
// Can't infer from input file name in this mode.
fmt . Fprintln ( os . Stderr , "[e] No function name specified." )
2012-06-07 21:34:34 +00:00
return
}
2012-06-07 21:28:16 +00:00
2012-06-07 21:34:34 +00:00
_ , file := path . Split ( * in )
file = strings . ToLower ( file )
file = strings . Replace ( file , " " , "_" , - 1 )
file = strings . Replace ( file , "." , "_" , - 1 )
file = strings . Replace ( file , "-" , "_" , - 1 )
if unicode . IsDigit ( rune ( file [ 0 ] ) ) {
// Identifier can't start with a digit.
file = "_" + file
2011-06-17 16:52:40 +00:00
}
2012-06-07 21:34:34 +00:00
fmt . Fprintf ( os . Stderr , "[w] No function name specified. Using '%s'.\n" , file )
* funcname = file
2011-06-17 15:44:59 +00:00
}
// Read the input file, transform it into a gzip compressed data stream and
// write it out as a go source file.
2011-06-17 16:52:40 +00:00
if pipe {
2012-06-13 12:17:25 +00:00
translate ( os . Stdin , os . Stdout , * pkgname , * funcname , * uncompressed )
2012-06-07 21:28:16 +00:00
fmt . Fprintln ( os . Stdout , "[i] Done." )
return
}
2011-11-02 15:40:26 +00:00
2012-06-07 21:28:16 +00:00
var fs , fd * os . File
var err error
2011-06-17 15:44:59 +00:00
2012-06-07 21:28:16 +00:00
if fs , err = os . Open ( * in ) ; err != nil {
fmt . Fprintf ( os . Stderr , "[e] %s\n" , err )
return
}
2011-11-02 15:40:26 +00:00
2012-06-07 21:28:16 +00:00
defer fs . Close ( )
2011-06-17 16:52:40 +00:00
2012-06-07 21:28:16 +00:00
if fd , err = os . Create ( * out ) ; err != nil {
fmt . Fprintf ( os . Stderr , "[e] %s\n" , err )
return
}
2012-03-27 23:39:01 +00:00
2012-06-07 21:28:16 +00:00
defer fd . Close ( )
2011-06-17 16:52:40 +00:00
2012-06-13 12:17:25 +00:00
translate ( fs , fd , * pkgname , * funcname , * uncompressed )
2012-06-07 21:28:16 +00:00
fmt . Fprintln ( os . Stdout , "[i] Done." )
2011-06-17 15:44:59 +00:00
}