-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
74 lines (63 loc) · 1.4 KB
/
main.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
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
)
func main() {
out, err := os.Create(os.Args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open %s for writing: %s\n", os.Args[1], err)
os.Exit(1)
}
fmt.Fprintf(out, `package static
// auto-generated by util/embed
func init() {
if Assets == nil {
Assets = make(map[string][]byte)
}
`)
for _, arg := range os.Args[2:] {
embedPath(arg, "", out)
}
fmt.Fprintf(out, "\n}\n")
}
func embedPath(path, prefix string, out io.Writer) {
st, err := os.Stat(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to stat %s: %s\n", path, err)
os.Exit(2)
}
if st.IsDir() {
files, err := ioutil.ReadDir(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to descend into %s: %s\n", path, err)
os.Exit(2)
}
for _, f := range files {
if strings.Index(f.Name(), ".") != 0 {
if prefix == "" {
embedPath(fmt.Sprintf("%s/%s", path, f.Name()), f.Name(), out)
} else {
embedPath(fmt.Sprintf("%s/%s", path, f.Name()), fmt.Sprintf("%s/%s", prefix, f.Name()), out)
}
}
}
return
}
bb, err := ioutil.ReadFile(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to read %s: %s\n", path, err)
os.Exit(2)
}
fmt.Fprintf(out, "\tAssets[\"/%s\"] = []byte{", prefix)
for i, b := range bb {
if i%16 == 0 {
fmt.Fprintf(out, "\n\t\t")
}
fmt.Fprintf(out, "%#x, ", b)
}
fmt.Fprintf(out, "\n\t}\n")
}