-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
254 lines (210 loc) · 4.77 KB
/
main.go
File metadata and controls
254 lines (210 loc) · 4.77 KB
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
253
254
package main
import (
"bytes"
"fmt"
"os"
"sort"
"strings"
"github.com/Urethramancer/signor/opt"
)
// version is filled in from Git tags by build scripts.
var version = "undefined"
var opts struct {
opt.DefaultHelp
Version bool `short:"V" help:"Show program version and exit."`
List string `short:"l" long:"list" help:"Name of a text file listing files to embed, one per line."`
Output string `short:"o" long:"output" help:"Output file name." default:"embed.go"`
Save bool `short:"s" long:"save" help:"Include save code in the output."`
Brotli bool `short:"b" long:"brotli" help:"Use Brotli compression instead of gzip."`
Files []string `placeholder:"PATH" help:"File or directory to embed."`
}
func main() {
a := opt.Parse(&opts)
if opts.Help || (opts.Files == nil && opts.List == "") {
if opts.Version {
pr("inc %s\n", version)
return
}
a.Usage()
return
}
var list []string
var err error
list = append(list, opts.Files...)
if opts.List != "" {
var l []string
l, err = loadList(opts.List)
if err != nil {
pr("Error loading list: %s\n", err.Error())
return
}
list = append(list, l...)
}
if len(list) == 0 {
return
}
sort.Strings(list)
convlist, err := ConvertFiles(list)
if err != nil {
pr("Error converting list: %s\n", err.Error())
return
}
var b bytes.Buffer
if opts.Brotli {
header = strings.Replace(header, "compress/gzip", "github.com/andybalholm/brotli", 1)
}
if opts.Save {
b.WriteString(header)
b.WriteString(save)
} else {
header = strings.Replace(header, fmtheader, "", 1)
b.WriteString(header)
}
if opts.Brotli {
b.WriteString(brotlidec)
} else {
b.WriteString(gzipdec)
}
b.WriteString(initfunc)
for _, v := range convlist {
s := fmt.Sprintf("\tembeddedFiles[\"%s\"] = &%s\n", v.Name, v.Path)
b.WriteString(s)
}
b.WriteString("}\n\n")
for _, f := range convlist {
_, err := b.Write(f.Data)
if err != nil {
pr("Couldn't write to buffer: %s", err.Error())
os.Exit(2)
}
}
err = saveString(b.String(), opts.Output)
if err != nil {
pr("Error saving output: %s\n", err.Error())
return
}
}
var (
fmtheader = ` "fmt"
`
header = `package main
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
var basePath = ""
// EmbeddedFileList holds the byte slices from original paths.
type EmbeddedFileList map[string]*[]byte
var embeddedFiles EmbeddedFileList
// SetBasePath sets the path prepended to file paths when checking for non-embedded files.
func SetBasePath(path string) {
basePath = path
}
// Exists checks if a file or directory exists.
func Exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
`
gzipdec = `// GetData decompresses an embedded gzip file.
// If a physical file exists at basePath+path, load that instead of the embedded file.
func GetData(fn string) ([]byte, error) {
p := filepath.Join(basePath, fn)
if Exists(p) {
return ioutil.ReadFile(p)
}
in, ok := embeddedFiles[fn]
if !ok {
return nil, os.ErrNotExist
}
gz, err := gzip.NewReader(bytes.NewBuffer(*in))
if err != nil {
return nil, err
}
var out bytes.Buffer
_, err = io.Copy(&out, gz)
err2 := gz.Close()
if err != nil {
return nil, err
}
if err2 != nil {
return nil, err2
}
return out.Bytes(), nil
}
`
brotlidec = `// GetData decompresses an embedded gzip file.
// If a physical file exists at basePath+path, load that instead of the embedded file.
func GetData(fn string) ([]byte, error) {
p := filepath.Join(basePath, fn)
if Exists(p) {
return ioutil.ReadFile(p)
}
in, ok := embeddedFiles[fn]
if !ok {
return nil, os.ErrNotExist
}
br := brotli.NewReader(bytes.NewBuffer(*in))
if br == nil {
return nil, io.ErrUnexpectedEOF
}
var out bytes.Buffer
_, err := io.Copy(&out, br)
if err != nil {
return nil, err
}
return out.Bytes(), nil
}
`
save = `// SaveData saves the specified embedded file relative to the specified path.
func SaveData(fn string) error {
data, ok := embeddedFiles[fn]
if !ok {
return fmt.Errorf("unknown embedded file %s", fn)
}
var err error
base := filepath.Dir(fn)
if !Exists(base) {
err = os.MkdirAll(base, 0755)
if err != nil {
return err
}
}
gz, err := gzip.NewReader(bytes.NewBuffer(*data))
if err != nil {
return err
}
f, err := os.Create(fn)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, gz)
gzerr := gz.Close()
if err != nil {
return err
}
return gzerr
}
// SaveAllData saves all embedded data, relative to the configured base path.
func SaveAllData() error {
for fn := range embeddedFiles {
out := filepath.Join(basePath, fn)
err := SaveData(out)
if err != nil {
return err
}
}
return nil
}
`
initfunc = `// init builds the list of embedded files.
func init() {
embeddedFiles = make(EmbeddedFileList)
`
)