This repository was archived by the owner on Jul 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
335 lines (312 loc) · 8.54 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// The pkgzip command bundles assets into a Go package.
//
// For more information see pkgzip -help
package main // import "code.soquee.net/pkgzip"
import (
"archive/zip"
"bytes"
"flag"
"fmt"
"go/format"
"go/parser"
"go/token"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"text/template"
"time"
)
// mtimeDate holds the arbitrary mtime that we assign to files when
// flagNoMtime is set.
var mtimeDate = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
const pkgTmpl = `// Package {{.Pkg}} is a virtual filesystem generated by the pkgzip command.
//
// To use this filesystem as an http.FileSystem see
// golang.org/x/tools/godoc/vfs/httpfs
package {{.Pkg}}
import (
"archive/zip"
"io"
"strings"
"golang.org/x/tools/godoc/vfs"
"golang.org/x/tools/godoc/vfs/zipfs"
)
var zipData = "{{.ByteLit}}"
// New returns the embedded filesystem.
func New() vfs.FileSystem {
br := strings.NewReader(zipData)
/* #nosec */
r, _ := zip.NewReader(br, int64(br.Len()))
rc := &zip.ReadCloser{
Reader: *r,
}
return zipfs.New(rc, "{{.Pkg}}")
}
// Reader returns a new reader that reads the mbedded filesystem in zip format.
func Reader() io.Reader {
return strings.NewReader(zipData)
}`
// Some exit codes. See sysexits(3).
const (
exCantCreat = 73
exDataErr = 65
exNoPerm = 77
exSoftware = 70
exUsage = 64
)
func main() {
logger := log.New(os.Stderr, "pkgzip ", 0)
debug := log.New(ioutil.Discard, "", 0)
var (
pkgName = "pkgzip"
pkgPath = "internal"
src = ""
force = false
noCompress = false
noMtime = false
v = false
)
flags := flag.NewFlagSet("pkgzip", flag.ContinueOnError)
flags.StringVar(&src, "src", src, "A directory to load files from")
flags.StringVar(&pkgName, "pkg", pkgName, "The name of the generated package")
flags.StringVar(&pkgPath, "dest", pkgPath, "The relative or absolute path to the generated package")
flags.BoolVar(&force, "f", force, "Overwrite the destination tree if it already exists.")
flags.BoolVar(&noCompress, "Z", noCompress, "Do not use compression to shrink the files.")
flags.BoolVar(&noMtime, "m", noMtime, "Ignore modification times on files.")
flags.BoolVar(&v, "v", v, "Use verbose error logging.")
err := flags.Parse(os.Args[1:])
if err != nil {
if err != flag.ErrHelp {
logger.Println(err)
}
os.Exit(exUsage)
}
if v {
debug = logger
}
pkgPath = path.Join(pkgPath, pkgName)
if pkgName == "" || src == "" {
logger.Println("Package name or src tree must not be empty.")
os.Exit(exDataErr)
}
if pkgPath == "" || pkgPath == "/" {
logger.Println("Package path should not be root. This could cause very bad things to happen.")
os.Exit(exDataErr)
}
tmp, err := ioutil.TempFile("", "pkgzip")
if err != nil {
logger.Printf("Error creating temp file: %q", err)
os.Exit(exCantCreat)
}
b, err := genZIP(debug, logger, noMtime, noCompress, src)
if err != nil {
logger.Printf("Error creating filesystem: %q", err)
os.Exit(exSoftware)
}
buf := &bytes.Buffer{}
err = template.Must(template.New("pkgzip").Parse(pkgTmpl)).Execute(buf, struct {
Pkg string
ByteLit string
}{
Pkg: pkgName,
ByteLit: b,
})
if err != nil {
logger.Printf("Error rendering generated code: %q", err)
os.Exit(exDataErr)
}
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, "", buf.String(), parser.ParseComments)
if err != nil {
logger.Printf("Error parsing generated code: %q", err)
os.Exit(exDataErr)
}
err = format.Node(tmp, fset, node)
if err != nil {
logger.Printf("Error formatting generated code: %q", err)
os.Exit(exDataErr)
}
err = tmp.Close()
if err != nil {
debug.Printf("Error closing temp file")
}
if force {
// If this fails, it either doesn't exist in which case everything is fine,
// or it does exist but we don't have permission to remove it but we might
// as well defer error handling until Rename tries to overwrite it and
// fails.
err = os.RemoveAll(pkgPath)
if err != nil {
debug.Printf("Error removing package at %q: %q", pkgPath, err)
}
}
err = os.MkdirAll(pkgPath, 0750)
if err != nil {
debug.Printf("Error creating package at %q: %q", pkgPath, err)
}
// Move the temp package to its final destination.
err = rename(debug, force, tmp.Name(), path.Join(pkgPath, "pkgzip.go"))
if err != nil {
logger.Printf("Error creating package at %q: %q", pkgPath, err)
err = os.RemoveAll(tmp.Name())
if err != nil {
logger.Printf("Error cleaning up temp files at %q: %q", tmp.Name(), err)
}
os.Exit(exNoPerm) // Probably… this may not be the actual problem.
}
}
// rename tries to os.Rename, but fall backs to copying from src
// to dest and unlink the source if os.Rename fails.
//
// This function was copied from statik and is used under the terms of the
// Apache license:
//
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
func rename(debug *log.Logger, force bool, src, dest string) error {
// Try to rename generated source.
if err := os.Rename(src, dest); err == nil {
return nil
}
// If the rename failed (might do so due to temporary file residing on a
// different device), try to copy byte by byte.
/* #nosec */
rc, err := os.Open(src)
if err != nil {
return err
}
defer func() {
err = rc.Close()
if err != nil {
debug.Printf("Error closing %q: %q", src, err)
}
err = os.Remove(src)
if err != nil {
debug.Printf("Error removing src %q: %q", src, err)
}
}()
if _, err = os.Stat(dest); !os.IsNotExist(err) {
if force {
if err = os.Remove(dest); err != nil {
return fmt.Errorf("file %q could not be deleted", dest)
}
} else {
return fmt.Errorf("file %q already exists; use -f to overwrite", dest)
}
}
wc, err := os.Create(dest)
if err != nil {
return err
}
defer wc.Close()
if _, err = io.Copy(wc, rc); err != nil {
// Delete remains of failed copy attempt.
err = os.Remove(dest)
if err != nil {
debug.Printf("Error removing failed copy attempt: %q", err)
}
}
return err
}
// Chunks of this function are copyright by Google and used under the terms of
// the Apache license. See rename() for details.
func genZIP(debug, logger *log.Logger, noMtime, noCompress bool, src string) (string, error) {
buf := &bytes.Buffer{}
w := zip.NewWriter(buf)
err := filepath.Walk(src, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
// Ignore directories and hidden files.
// No entry is needed for directories in a zip file.
// Each file is represented with a path, no directory
// entities are required to build the hierarchy.
if fi.IsDir() || strings.HasPrefix(fi.Name(), ".") {
return nil
}
relPath, err := filepath.Rel(src, path)
if err != nil {
return err
}
/* #nosec */
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
fHeader, err := zip.FileInfoHeader(fi)
if err != nil {
return err
}
if noMtime {
// Always use the same modification time so that
// the output is deterministic with respect to the file contents.
fHeader.SetModTime(mtimeDate)
}
fHeader.Name = filepath.ToSlash(relPath)
if !noCompress {
fHeader.Method = zip.Deflate
}
f, err := w.CreateHeader(fHeader)
if err != nil {
return err
}
_, err = f.Write(b)
return err
})
if err != nil {
return "", err
}
err = w.Close()
s := &strings.Builder{}
for _, b := range buf.Bytes() {
if b == '\n' {
_, err = s.WriteString(`\n`)
if err != nil {
logger.Println("Error writing to output string")
os.Exit(exSoftware)
}
continue
}
if b == '\\' {
_, err = s.WriteString(`\\`)
if err != nil {
logger.Println("Error writing to output string")
os.Exit(exSoftware)
}
continue
}
if b == '"' {
_, err = s.WriteString(`\"`)
if err != nil {
logger.Println("Error writing to output string")
os.Exit(exSoftware)
}
continue
}
if (b >= 32 && b <= 126) || b == '\t' {
err = s.WriteByte(b)
if err != nil {
logger.Println("Error writing byte to output string")
os.Exit(exSoftware)
}
continue
}
fmt.Fprintf(s, "\\x%02x", b)
}
return s.String(), err
}