-
Notifications
You must be signed in to change notification settings - Fork 25
/
debug.go
129 lines (107 loc) · 2.71 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
120
121
122
123
124
125
126
127
128
129
// Copyright 2018 The go-bindata Authors. All rights reserved.
// Use of this source code is governed by a CC0 1.0 Universal (CC0 1.0)
// Public Domain Dedication license that can be found in the LICENSE file.
package bindata
import (
"fmt"
"io"
"path/filepath"
)
// writeOneFileDebug writes the debug code file for each file (when splited file).
func writeOneFileDebug(w io.Writer, c *Config, ast *asset) error {
if err := writeDebugFileHeader(w, c.Dev); err != nil {
return err
}
if err := writeDebugAsset(w, c, ast); err != nil {
return err
}
return nil
}
// writeDebug writes the debug code file for single file.
func writeDebug(w io.Writer, c *Config, keys []string, toc map[string]*asset) error {
err := writeDebugHeader(w)
if err != nil {
return err
}
for _, key := range keys {
ast := toc[key]
err = writeDebugAsset(w, c, ast)
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, ast *asset) error {
pathExpr := fmt.Sprintf("%q", filepath.Join(c.cwd, ast.path))
if c.Dev {
pathExpr = fmt.Sprintf("filepath.Join(rootDir, %q)", ast.name)
}
_, err := fmt.Fprintf(w, `// %s reads file data from disk. It returns an error on failure.
func %sBytes() ([]byte, error) {
asset, err := %s()
if asset == nil {
return nil, err
}
return asset.bytes, err
}
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
}
`, ast.funcName, ast.funcName, ast.funcName, ast.funcName, pathExpr, ast.name)
return err
}