diff --git a/bytewriter.go b/lib/bytewriter.go similarity index 97% rename from bytewriter.go rename to lib/bytewriter.go index 5c39e57..c9b602c 100644 --- a/bytewriter.go +++ b/lib/bytewriter.go @@ -2,7 +2,7 @@ // license. Its contents can be found at: // http://creativecommons.org/publicdomain/zero/1.0/ -package main +package bindata import ( "fmt" diff --git a/stringwriter.go b/lib/stringwriter.go similarity index 96% rename from stringwriter.go rename to lib/stringwriter.go index 40ffe93..278f35b 100644 --- a/stringwriter.go +++ b/lib/stringwriter.go @@ -2,7 +2,7 @@ // license. Its contents can be found at: // http://creativecommons.org/publicdomain/zero/1.0/ -package main +package bindata import ( "fmt" diff --git a/toc.go b/lib/toc.go similarity index 75% rename from toc.go rename to lib/toc.go index 54c73b7..a534d93 100644 --- a/toc.go +++ b/lib/toc.go @@ -2,7 +2,7 @@ // license. Its contents can be found at: // http://creativecommons.org/publicdomain/zero/1.0/ -package main +package bindata import ( "fmt" @@ -13,7 +13,7 @@ import ( ) // createTOC writes a table of contents file to the given location. -func createTOC(dir, pkgname string) error { +func CreateTOC(dir, pkgname string) error { file := filepath.Join(dir, "bindata-toc.go") code := fmt.Sprintf(`package %s @@ -26,8 +26,9 @@ var go_bindata = make(map[string]func() []byte)`, pkgname) return ioutil.WriteFile(file, []byte(code), 0600) } -// writeTOCInit writes the TOC init function for a given data file. -func writeTOCInit(output io.Writer, filename, prefix, funcname string) { +// WriteTOCInit writes the TOC init function for a given data file +// replacing the prefix in the filename by "", funcname being the translated function name +func WriteTOCInit(output io.Writer, filename, prefix, funcname string) { filename = strings.Replace(filename, prefix, "", 1) fmt.Fprintf(output, ` diff --git a/translate.go b/lib/translate.go similarity index 80% rename from translate.go rename to lib/translate.go index 6b1d973..4e8cb2f 100644 --- a/translate.go +++ b/lib/translate.go @@ -2,16 +2,21 @@ // license. Its contents can be found at: // http://creativecommons.org/publicdomain/zero/1.0/ -package main +package bindata import ( "compress/gzip" "fmt" "io" + "regexp" + "strings" + "unicode" ) +var regFuncName = regexp.MustCompile(`[^a-zA-Z0-9_]`) + // translate translates the input file to go source code. -func translate(input io.Reader, output io.Writer, pkgname, funcname string, uncompressed, nomemcpy bool) { +func Translate(input io.Reader, output io.Writer, pkgname, funcname string, uncompressed, nomemcpy bool) { if nomemcpy { if uncompressed { translate_nomemcpy_uncomp(input, output, pkgname, funcname) @@ -151,3 +156,32 @@ func %s() []byte { } `, funcname, funcname, funcname, funcname) } + +// safeFuncname creates a safe function name from the input path. +func SafeFuncname(in, prefix string) string { + name := strings.Replace(in, prefix, "", 1) + + if len(name) == 0 { + name = in + } + + name = strings.ToLower(name) + name = regFuncName.ReplaceAllString(name, "_") + + if unicode.IsDigit(rune(name[0])) { + // Identifier can't start with a digit. + name = "_" + name + } + + // Get rid of "__" instances for niceness. + for strings.Index(name, "__") > -1 { + name = strings.Replace(name, "__", "_", -1) + } + + // Leading underscore is silly. + if name[0] == '_' { + name = name[1:] + } + + return name +} diff --git a/version.go b/lib/version.go similarity index 97% rename from version.go rename to lib/version.go index 1fc2edd..38d44c2 100644 --- a/version.go +++ b/lib/version.go @@ -2,7 +2,7 @@ // license. Its contents can be found at: // http://creativecommons.org/publicdomain/zero/1.0/ -package main +package bindata import ( "fmt" diff --git a/main.go b/main.go index 718edd6..a8bcd68 100644 --- a/main.go +++ b/main.go @@ -7,11 +7,10 @@ package main import ( "flag" "fmt" + "github.com/jteeuwen/go-bindata/lib" "os" "path" "path/filepath" - "regexp" - "strings" "unicode" ) @@ -27,14 +26,13 @@ var ( tags = flag.String("tags", "", "Optional build tags") toc = flag.Bool("toc", false, "Generate a table of contents for this and other files. The input filepath becomes the map key. This option is only useable in non-pipe mode.") version = flag.Bool("version", false, "Display version information.") - regFuncName = regexp.MustCompile(`[^a-zA-Z0-9_]`) ) func main() { parseArgs() if pipe { - translate(os.Stdin, os.Stdout, *pkgname, *funcname, *uncompressed, *nomemcopy) + bindata.Translate(os.Stdin, os.Stdout, *pkgname, *funcname, *uncompressed, *nomemcopy) return } @@ -59,20 +57,20 @@ func main() { } // Translate binary to Go code. - translate(fs, fd, *pkgname, *funcname, *uncompressed, *nomemcopy) + bindata.Translate(fs, fd, *pkgname, *funcname, *uncompressed, *nomemcopy) // Append the TOC init function to the end of the output file and // write the `bindata-toc.go` file, if applicable. if *toc { dir, _ := filepath.Split(*out) - err := createTOC(dir, *pkgname) + err := bindata.CreateTOC(dir, *pkgname) if err != nil { fmt.Fprintf(os.Stderr, "[e] %s\n", err) return } - writeTOCInit(fd, in, *prefix, *funcname) + bindata.WriteTOCInit(fd, in, "", *funcname) } } @@ -85,7 +83,7 @@ func parseArgs() { flag.Parse() if *version { - fmt.Printf("%s\n", Version()) + fmt.Printf("%s\n", bindata.Version()) os.Exit(0) } @@ -114,7 +112,7 @@ func parseArgs() { os.Exit(1) } - *funcname = safeFuncname(in, *prefix) + *funcname = bindata.SafeFuncname(in, *prefix) fmt.Fprintf(os.Stderr, "[w] No function name specified. Using %s.\n", *funcname) } } @@ -157,32 +155,3 @@ func safeFilename(out, in string) string { return filename } - -// safeFuncname creates a safe function name from the input path. -func safeFuncname(in, prefix string) string { - name := strings.Replace(in, prefix, "", 1) - - if len(name) == 0 { - name = in - } - - name = strings.ToLower(name) - name = regFuncName.ReplaceAllString(name, "_") - - if unicode.IsDigit(rune(name[0])) { - // Identifier can't start with a digit. - name = "_" + name - } - - // Get rid of "__" instances for niceness. - for strings.Index(name, "__") > -1 { - name = strings.Replace(name, "__", "_", -1) - } - - // Leading underscore is silly. - if name[0] == '_' { - name = name[1:] - } - - return name -}