-
Notifications
You must be signed in to change notification settings - Fork 21
/
debugpack.go
69 lines (60 loc) · 1.36 KB
/
debugpack.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
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
)
// Pack debug package into a single file to be included by godebug.
// This file is called from the godebug pkg with "//go:generate"
func main() {
if err := main2(); err != nil {
fmt.Println(err)
}
}
func main2() error {
dir := "debug"
fis, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
filenames := []string{}
data := []string{}
for _, fi := range fis {
// don't add test files to pack
if strings.HasSuffix(fi.Name(), "_test.go") {
continue
}
filename := filepath.Join(dir, fi.Name())
b, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
filenames = append(filenames, fi.Name())
data = append(data, strconv.QuoteToASCII(string(b)))
}
if err := buildDataFile(filenames, data, "zdebugpack.go"); err != nil {
return err
}
return nil
}
func buildDataFile(filenames []string, data []string, filename string) error {
z := []string{}
for i := range filenames {
e := fmt.Sprintf("{%q, %s}", filenames[i], data[i])
z = append(z, e)
}
w := `package godebug
// DO NOT EDIT: code generated by debugpack/debugpack.go
type FilePack struct {
Name string
Data string
}
func DebugFilePacks() []*FilePack {
return []*FilePack{%s}
}
`
u := fmt.Sprintf(w, strings.Join(z, ",\n\t\t"))
return ioutil.WriteFile(filename, []byte(u), 0644)
}