2013-10-30 01:11:04 +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/
|
|
|
|
|
|
|
|
package bindata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
// writeDebug writes the debug code file.
|
2015-01-26 23:45:06 +00:00
|
|
|
func writeDebug(w io.Writer, c *Config, toc []Asset) error {
|
2013-10-30 13:45:04 +00:00
|
|
|
err := writeDebugHeader(w)
|
2013-10-30 12:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range toc {
|
2015-01-26 23:45:06 +00:00
|
|
|
err = writeDebugAsset(w, c, &toc[i])
|
2013-10-30 12:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-30 01:16:14 +00:00
|
|
|
// writeDebugHeader writes output file headers.
|
2013-10-30 01:11:04 +00:00
|
|
|
// This targets debug builds.
|
2013-10-30 13:45:04 +00:00
|
|
|
func writeDebugHeader(w io.Writer) error {
|
|
|
|
_, err := fmt.Fprintf(w, `import (
|
2013-11-08 15:38:45 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2014-12-07 22:31:35 +00:00
|
|
|
"os"
|
2014-12-08 00:30:11 +00:00
|
|
|
"path/filepath"
|
2015-10-23 07:06:06 +00:00
|
|
|
"strings"
|
2013-10-30 01:11:04 +00:00
|
|
|
)
|
|
|
|
|
2015-05-11 19:42:14 +00:00
|
|
|
// bindataRead reads the given file from disk. It returns an error on failure.
|
|
|
|
func bindataRead(path, name string) ([]byte, error) {
|
2013-11-08 15:38:45 +00:00
|
|
|
buf, err := ioutil.ReadFile(path)
|
2013-10-30 01:11:04 +00:00
|
|
|
if err != nil {
|
2013-11-08 15:38:45 +00:00
|
|
|
err = fmt.Errorf("Error reading asset %%s at %%s: %%v", name, path, err)
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
2013-11-08 15:38:45 +00:00
|
|
|
return buf, err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 22:31:35 +00:00
|
|
|
type asset struct {
|
|
|
|
bytes []byte
|
|
|
|
info os.FileInfo
|
|
|
|
}
|
|
|
|
|
2013-10-30 01:11:04 +00:00
|
|
|
`)
|
2013-10-30 12:14:58 +00:00
|
|
|
return err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 12:14:58 +00:00
|
|
|
// writeDebugAsset write a debug entry for the given asset.
|
2013-10-30 01:11:04 +00:00
|
|
|
// A debug entry is simply a function which reads the asset from
|
|
|
|
// the original file (e.g.: from disk).
|
2015-01-26 23:45:06 +00:00
|
|
|
func writeDebugAsset(w io.Writer, c *Config, asset *Asset) error {
|
|
|
|
pathExpr := fmt.Sprintf("%q", asset.Path)
|
|
|
|
if c.Dev {
|
|
|
|
pathExpr = fmt.Sprintf("filepath.Join(rootDir, %q)", asset.Name)
|
|
|
|
}
|
|
|
|
|
2014-05-28 12:43:02 +00:00
|
|
|
_, err := fmt.Fprintf(w, `// %s reads file data from disk. It returns an error on failure.
|
2014-12-07 22:31:35 +00:00
|
|
|
func %s() (*asset, error) {
|
2015-01-26 23:45:06 +00:00
|
|
|
path := %s
|
2014-12-07 22:31:35 +00:00
|
|
|
name := %q
|
2015-05-11 19:42:14 +00:00
|
|
|
bytes, err := bindataRead(path, name)
|
2014-12-07 22:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error reading asset info %%s at %%s: %%v", name, path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a := &asset{bytes: bytes, info: fi}
|
|
|
|
return a, err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|
2014-05-28 12:43:02 +00:00
|
|
|
|
2015-01-26 23:45:06 +00:00
|
|
|
`, asset.Func, asset.Func, pathExpr, asset.Name)
|
2013-10-30 12:14:58 +00:00
|
|
|
return err
|
2013-10-30 01:11:04 +00:00
|
|
|
}
|