diff --git a/asset.go b/asset.go
index 6f9e50d..95b6b94 100644
--- a/asset.go
+++ b/asset.go
@@ -4,7 +4,7 @@
package bindata
-// File is an asset entry for the table of contents.
+// Asset holds information about a single asset to be processed.
type Asset struct {
Path string // Full file path.
Name string // Key used in TOC -- name by which asset is referenced.
diff --git a/config.go b/config.go
index e27a860..559930f 100644
--- a/config.go
+++ b/config.go
@@ -4,6 +4,12 @@
package bindata
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+)
+
// Config defines a set of options for the asset conversion.
type Config struct {
// Name of the package to use. Defaults to 'main'.
@@ -20,7 +26,9 @@ type Config struct {
// conversion.
Input string
- // Output defines the output directory for the generated code.
+ // Output defines the output file for the generated code.
+ // If left empty, this defaults to 'bindata.go' in the current
+ // working directory.
Output string
/*
@@ -87,11 +95,22 @@ type Config struct {
NoMemCopy bool
/*
- Compress means the assets are GZIP compressed before being turned
+ NoCompress means the assets are /not/ GZIP compressed before being turned
into Go code. The generated function will automatically unzip
the file data when called. Defaults to true.
*/
- Compress bool
+ NoCompress bool
+
+ // Perform a debug build. This generates an asset file, which
+ // loads the asset contents directly from disk at their original
+ // location, instead of embedding the contents in the code.
+ //
+ // This is mostly useful if you anticipate that the assets are
+ // going to change during your development cycle. You will always
+ // want your code to access the latest version of the asset.
+ // Only in release mode, will the assets actually be embedded
+ // in the code. The default behaviour is Release mode.
+ Debug bool
}
// NewConfig returns a default configuration struct.
@@ -99,6 +118,44 @@ func NewConfig() *Config {
c := new(Config)
c.Package = "main"
c.NoMemCopy = false
- c.Compress = true
+ c.NoCompress = false
+ c.Debug = false
return c
}
+
+// validate ensures the config has sane values.
+// Part of which means checking if certain file/directory paths exist.
+func (c *Config) validate() error {
+ if len(c.Package) == 0 {
+ return fmt.Errorf("Missing package name")
+ }
+
+ stat, err := os.Lstat(c.Input)
+ if err != nil {
+ return fmt.Errorf("Input path: %v", err)
+ }
+
+ if !stat.IsDir() {
+ return fmt.Errorf("Input path is not a directory.")
+ }
+
+ if len(c.Output) == 0 {
+ cwd, err := os.Getwd()
+ if err != nil {
+ return fmt.Errorf("Unable to determine current working directory.")
+ }
+
+ c.Output = filepath.Join(cwd, "bindata.go")
+ }
+
+ stat, err = os.Lstat(c.Output)
+ if err != nil && !os.IsNotExist(err) {
+ return fmt.Errorf("Output path: %v", err)
+ }
+
+ if stat != nil && stat.IsDir() {
+ return fmt.Errorf("Output path is a directory.")
+ }
+
+ return nil
+}
diff --git a/convert.go b/convert.go
index d91181b..2609a07 100644
--- a/convert.go
+++ b/convert.go
@@ -13,38 +13,59 @@ import (
"unicode"
)
-// ProgressFunc is a callback handler which is fired whenever
-// bindata begins translating a new file.
-//
-// It takes the file path and the current and total file counts.
-// This can be used to indicate progress of the conversion.
-//
-// If this handler returns true, the processing is stopped and
-// Translate() returns immediately.
-type ProgressFunc func(file string, current, total int) bool
-
// Translate reads assets from an input directory, converts them
-// to Go code and writes new files to the output directory specified
+// to Go code and writes new files to the output file specified
// in the given configuration.
-func Translate(c *Config, pf ProgressFunc) error {
+func Translate(c *Config) error {
var toc []Asset
- err := findFiles(c.Input, c.Prefix, &toc)
+ // Ensure our configuration has sane values.
+ err := c.validate()
if err != nil {
return err
}
- err = writeDebug(c, toc)
+ // Locate all the assets.
+ err = findFiles(c.Input, c.Prefix, &toc)
if err != nil {
return err
}
- err = writeRelease(c, toc)
+ // Create output file.
+ fd, err := os.Create(c.Output)
if err != nil {
return err
}
- return writeTOC(c, toc)
+ defer fd.Close()
+
+ // Write build tags, if applicable.
+ if len(c.Tags) > 0 {
+ _, err = fmt.Fprintf(fd, "// +build %s\n\n", c.Tags)
+ if err != nil {
+ return err
+ }
+ }
+
+ // Write package declaration.
+ _, err = fmt.Fprintf(fd, "package %s\n\n", c.Package)
+ if err != nil {
+ return err
+ }
+
+ // Write assets.
+ if c.Debug {
+ err = writeDebug(fd, toc)
+ } else {
+ err = writeRelease(fd, c, toc)
+ }
+
+ if err != nil {
+ return err
+ }
+
+ // Write table of contents
+ return writeTOC(fd, toc)
}
// fillTOC recursively finds all the file paths in the given directory tree.
diff --git a/debug.go b/debug.go
index 43404b3..393c311 100644
--- a/debug.go
+++ b/debug.go
@@ -7,26 +7,17 @@ package bindata
import (
"fmt"
"io"
- "os"
- "path/filepath"
)
// writeDebug writes the debug code file.
-func writeDebug(c *Config, toc []Asset) error {
- fd, err := os.Create(filepath.Join(c.Output, "bindata_debug.go"))
- if err != nil {
- return err
- }
-
- defer fd.Close()
-
- err = writeDebugHeader(fd, c)
+func writeDebug(w io.Writer, toc []Asset) error {
+ err := writeDebugHeader(w)
if err != nil {
return err
}
for i := range toc {
- err = writeDebugAsset(fd, c, &toc[i])
+ err = writeDebugAsset(w, &toc[i])
if err != nil {
return err
}
@@ -37,30 +28,8 @@ func writeDebug(c *Config, toc []Asset) error {
// writeDebugHeader writes output file headers.
// This targets debug builds.
-func writeDebugHeader(w io.Writer, c *Config) error {
- var err error
-
- // Write build tags, if applicable.
- if len(c.Tags) > 0 {
- _, err = fmt.Fprintf(w, "// +build !release %s\n\n", c.Tags)
- } else {
- _, err = fmt.Fprintf(w, "// +build !release\n\n")
- }
-
- if err != nil {
- return err
- }
-
- // Write package declaration
- _, err = fmt.Fprintf(w, "package %s\n\n", c.Package)
- if err != nil {
- return err
- }
-
- // Define packages we need to import.
- // And add the asset_read function. This is called
- // from asset-specific functions.
- _, err = fmt.Fprintf(w, `import (
+func writeDebugHeader(w io.Writer) error {
+ _, err := fmt.Fprintf(w, `import (
"bytes"
"io"
"log"
@@ -93,14 +62,16 @@ func bindata_read(path, name string) []byte {
// writeDebugAsset write a debug entry for the given asset.
// A debug entry is simply a function which reads the asset from
// the original file (e.g.: from disk).
-func writeDebugAsset(w io.Writer, c *Config, asset *Asset) error {
- _, err := fmt.Fprintf(w, `func %s() []byte {
+func writeDebugAsset(w io.Writer, asset *Asset) error {
+ _, err := fmt.Fprintf(w, `
+// %s reads file data from disk.
+// It panics if something went wrong in the process.
+func %s() []byte {
return bindata_read(
%q,
%q,
)
}
-
-`, asset.Func, asset.Path, asset.Name)
+`, asset.Func, asset.Func, asset.Path, asset.Name)
return err
}
diff --git a/go-bindata/main.go b/go-bindata/main.go
index 900aaf0..d0dbc45 100644
--- a/go-bindata/main.go
+++ b/go-bindata/main.go
@@ -13,11 +13,11 @@ import (
)
func main() {
- cfg, status := parseArgs()
- err := bindata.Translate(cfg, status)
+ cfg := parseArgs()
+ err := bindata.Translate(cfg)
if err != nil {
- fmt.Fprintf(os.Stderr, "bindata: %v", err)
+ fmt.Fprintf(os.Stderr, "bindata: %v\n", err)
}
}
@@ -26,23 +26,23 @@ func main() {
//
// This function exits the program with an error, if
// any of the command line options are incorrect.
-func parseArgs() (*bindata.Config, bindata.ProgressFunc) {
- var version, quiet bool
+func parseArgs() *bindata.Config {
+ var version bool
c := bindata.NewConfig()
flag.Usage = func() {
- fmt.Printf("Usage: %s [options] [