-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
252 lines (202 loc) · 4.41 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"text/template"
)
func main() {
dir := flag.String("dir", "", "The path of the directory to embed")
pkgName := flag.String("pkgname", "", "The name of the package to generate")
out := flag.String("o", "-", "output file")
flag.Parse()
if *dir == "" {
fmt.Fprintf(os.Stderr, "missing -dir\n")
os.Exit(1)
}
if *pkgName == "" {
fmt.Fprintf(os.Stderr, "missing -pkgname\n")
os.Exit(1)
}
archiveBytes, err := getArchiveBytes(*dir)
if err != nil {
fmt.Fprintf(os.Stderr, "error generating package %s from %s: %s\n", *pkgName, *dir, err)
os.Exit(1)
}
outFile := os.Stdout
if *out != "-" {
outFile, err = os.Create(*out)
if err != nil {
fmt.Fprintf(os.Stderr, "error opening output file: %s\n", err)
os.Exit(1)
}
}
err = writePackage(outFile, *pkgName, archiveBytes)
if err != nil {
fmt.Fprintf(os.Stderr, "error writing package: %s\n", *pkgName, err)
os.Exit(1)
}
err = outFile.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "error closing output %s\n", err)
os.Exit(1)
}
}
func getArchiveBytes(dir string) ([]byte, error) {
dir, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
pathSep := fmt.Sprintf("%c", filepath.Separator)
if !strings.HasSuffix(dir, pathSep) {
dir += pathSep
}
buf := bytes.Buffer{}
w, err := gzip.NewWriterLevel(&buf, gzip.BestCompression)
if err != nil {
return nil, err
}
tw := tar.NewWriter(w)
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
hdr, err := tar.FileInfoHeader(info, "")
if err != nil {
return err
}
hdr.Name = filepath.ToSlash(path[len(dir)-1:])
err = tw.WriteHeader(hdr)
if err != nil {
return err
}
_, err = io.Copy(tw, f)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, fmt.Errorf("error walking input directive: %s", err)
}
err = tw.Close()
if err != nil {
return nil, fmt.Errorf("error creating archive: %s", err)
}
err = w.Close()
if err != nil {
return nil, fmt.Errorf("closing gzip stream: %s", err)
}
return buf.Bytes(), nil
}
func bytesToGoString(data []byte) string {
buf := bytes.Buffer{}
for _, b := range data {
_, _ = fmt.Fprintf(&buf, "\\x%02x", b)
}
return `"` + buf.String() + `"`
}
func writePackage(w io.Writer, pkgName string, archive []byte) error {
err := packageTemplate.Execute(w, struct {
PkgName string
DataString string
}{
PkgName: pkgName,
DataString: bytesToGoString(archive),
})
return err
}
var packageTemplate *template.Template
var packageTemplateText string = `// This file was generated by dir2go, do not modify
package {{.PkgName}}
import (
"archive/tar"
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"net/http"
"os"
)
type EmbeddedFile struct {
Info os.FileInfo
Contents []byte
}
type EmbeddedFileHandle struct {
File *EmbeddedFile
R *bytes.Reader
}
func (h *EmbeddedFileHandle) Read(buf []byte) (int, error) {
return h.R.Read(buf)
}
func (h *EmbeddedFileHandle) Seek(offset int64, whence int) (int64, error) {
return h.R.Seek(offset, whence)
}
func (h *EmbeddedFileHandle) Close() error {
return nil
}
func (h *EmbeddedFileHandle) Readdir(count int) ([]os.FileInfo, error) {
return nil, os.ErrPermission
}
func (h *EmbeddedFileHandle) Stat() (os.FileInfo, error) {
return h.File.Info, nil
}
type FileSystem map[string]*EmbeddedFile
func (fs FileSystem) Open(name string) (http.File, error) {
f, ok := fs[name]
if !ok {
return nil, os.ErrNotExist
}
return &EmbeddedFileHandle{
File: f,
R: bytes.NewReader(f.Contents),
}, nil
}
func LoadFiles() {
Files = make(FileSystem)
gz, err := gzip.NewReader(bytes.NewBuffer([]byte(data)))
if err != nil {
panic(err)
}
tr := tar.NewReader(gz)
for {
hdr, err := tr.Next()
if err != nil {
if err == io.EOF {
break
}
panic(err)
}
if hdr.Typeflag != tar.TypeReg {
continue
}
contents, err := ioutil.ReadAll(tr)
if err != nil {
panic(err)
}
Files[hdr.Name] = &EmbeddedFile{
Info: hdr.FileInfo(),
Contents: contents,
}
}
}
var Files FileSystem
var data string = {{.DataString}}
`
func init() {
packageTemplate = template.Must(template.New("package").Parse(packageTemplateText))
}