forked from shuLhan/go-bindata
-
Notifications
You must be signed in to change notification settings - Fork 2
/
debug.go
119 lines (98 loc) · 2.47 KB
/
debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// 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"
)
// writeOneFileDebug writes the debug code file for each file (when splited file).
func writeOneFileDebug(w io.Writer, c *Config, a *Asset) error {
if err := writeDebugFileHeader(w, c.Dev); err != nil {
return err
}
if err := writeDebugAsset(w, c, a); err != nil {
return err
}
return nil
}
// writeDebug writes the debug code file for single file.
func writeDebug(w io.Writer, c *Config, toc []Asset) error {
err := writeDebugHeader(w)
if err != nil {
return err
}
for i := range toc {
err = writeDebugAsset(w, c, &toc[i])
if err != nil {
return err
}
}
return nil
}
// writeDebugHeader writes output file headers for each file.
// This targets debug builds.
func writeDebugFileHeader(w io.Writer, dev bool) error {
add := ""
if dev {
add = `
"path/filepath"`
}
_, err := fmt.Fprintf(w, `import (
"fmt"
"os"%s
)
`, add)
return err
}
// writeDebugHeader writes output file headers for sigle file.
// This targets debug builds.
func writeDebugHeader(w io.Writer) error {
_, err := fmt.Fprintf(w, `import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// bindataRead reads the given file from disk. It returns an error on failure.
func bindataRead(path, name string) ([]byte, error) {
buf, err := ioutil.ReadFile(path)
if err != nil {
err = fmt.Errorf("Error reading asset %%s at %%s: %%v", name, path, err)
}
return buf, err
}
type asset struct {
bytes []byte
info os.FileInfo
}
`)
return err
}
// 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 {
pathExpr := fmt.Sprintf("\"%s/%s\"", c.cwd, asset.Path)
if c.Dev {
pathExpr = fmt.Sprintf("filepath.Join(rootDir, %q)", asset.Name)
}
_, err := fmt.Fprintf(w, `// %s reads file data from disk. It returns an error on failure.
func %s() (*asset, error) {
path := %s
name := %q
bytes, err := bindataRead(path, name)
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
}
`, asset.Func, asset.Func, pathExpr, asset.Name)
return err
}