-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
240 lines (181 loc) · 5.44 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
package main
import (
"bufio"
"bytes"
"compress/gzip"
"encoding/base64"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
var (
flagStaticDir = flag.String("i", "static", "Static directory to embed")
flagOuputFile = flag.String("o", "", "Output file to write to")
flagPkg = flag.String("pkg", "main", "Package name of the generated static file")
flagGroup = flag.String("group", "Assets", "The group name of the static files i.e. CSS, JS, Assets, HTML. It will be added to the generated static function name.")
flagIgnore = flag.String("ignore", "", "Regexp for files/dirs we should ignore i.e. \\.gitignore")
flagPrefix = flag.String("prefix", "", "Prefix to strip from file paths")
flagInit = flag.Bool("init", false, " determines if only initializing the static file without contents")
ignoreRegexp *regexp.Regexp
writer *bufio.Writer
)
func main() {
parseFlags()
os.Remove(*flagOuputFile)
f, err := os.Create(*flagOuputFile)
if err != nil {
log.Panic(err)
}
defer f.Close()
funcName := strings.ToUpper((*flagGroup)[0:1]) + (*flagGroup)[1:]
writer = bufio.NewWriter(f)
ignoreFlag := ""
prefixFlag := ""
if len(*flagIgnore) > 0 {
ignoreFlag = "-ignore=" + *flagIgnore
}
if len(*flagPrefix) > 0 {
prefixFlag = "-prefix=" + *flagPrefix
}
if *flagInit {
writer.WriteString(fmt.Sprintf(initStartFile, *flagStaticDir, *flagOuputFile, *flagPkg, *flagGroup, ignoreFlag, prefixFlag, *flagPkg, funcName, funcName))
writer.WriteString(initEndfile)
} else {
writer.WriteString(fmt.Sprintf(startFile, *flagStaticDir, *flagOuputFile, *flagPkg, *flagGroup, ignoreFlag, prefixFlag, *flagPkg, funcName, funcName))
processFiles(filepath.Clean(*flagStaticDir))
writer.WriteString(endfile)
}
writer.Flush()
f.Close()
// after file written run gofmt on file
cmd := exec.Command("gofmt", "-s", "-w", *flagOuputFile)
if err = cmd.Run(); err != nil {
log.Panic(err)
}
}
func parseFlags() {
flag.Parse()
s := filepath.Clean(*flagStaticDir)
flagStaticDir = &s
if len(*flagStaticDir) == 0 || *flagStaticDir == "." {
panic("**invalid Static File Directoy '" + *flagStaticDir + "'")
}
if len(*flagOuputFile) == 0 {
panic("**invalid Output Directory")
}
if len(*flagPkg) == 0 {
panic("**invalid Package Name")
}
if len(*flagIgnore) > 0 {
var err error
ignoreRegexp, err = regexp.Compile(*flagIgnore)
if err != nil {
panic("**Error Compiling Regex:" + err.Error())
}
}
}
func processFiles(dir string) {
fi, err := os.Stat(dir)
if err != nil {
log.Panic(err)
}
dPath := applyPathOptions(dir)
writer.WriteString(fmt.Sprintf(dirFileStart, dPath, fi.Name(), fi.Size(), fi.Mode(), fi.ModTime().Unix(), true, ""))
processFilesRecursive(dir, "", false, "")
writer.WriteString(dirFileEnd)
}
// need isSymlinkDir variable as it is valid for symlinkDir to be blank
func processFilesRecursive(path string, dir string, isSymlinkDir bool, symlinkDir string) {
var b64File string
var p string
var tmpPath string
f, err := os.Open(path)
if err != nil {
log.Panic(err)
}
files, err := f.Readdir(0)
for _, file := range files {
info := file
b64File = ""
p = path + string(os.PathSeparator) + file.Name()
fPath := p
if isSymlinkDir {
fPath = strings.Replace(p, dir, symlinkDir, 1)
}
if ignoreRegexp != nil && ignoreRegexp.MatchString(fPath) {
continue
}
if file.IsDir() {
tmpPath = applyPathOptions(fPath)
fmt.Println("Processing:", tmpPath)
// write out here
writer.WriteString(fmt.Sprintf(dirFileStart, tmpPath, info.Name(), info.Size(), info.Mode(), info.ModTime().Unix(), true, ""))
processFilesRecursive(p, p, isSymlinkDir, symlinkDir+string(os.PathSeparator)+info.Name())
writer.WriteString(dirFileEndArray)
continue
}
if file.Mode()&os.ModeSymlink == os.ModeSymlink {
link, err := filepath.EvalSymlinks(p)
if err != nil {
log.Panic("Error Resolving Symlink", err)
}
fi, err := os.Stat(link)
if err != nil {
log.Panic(err)
}
info = fi
if fi.IsDir() {
tmpPath = applyPathOptions(fPath)
fmt.Println("Processing:", tmpPath)
// write out here
writer.WriteString(fmt.Sprintf(dirFileStart, tmpPath, file.Name(), info.Size(), info.Mode(), info.ModTime().Unix(), true, ""))
processFilesRecursive(link, link, true, fPath)
writer.WriteString(dirFileEndArray)
continue
}
}
// if we get here it's a file
// read file
b, err := ioutil.ReadFile(p)
if err != nil {
log.Panic(err)
}
// gzip
var gzBuff bytes.Buffer
gz := gzip.NewWriter(&gzBuff)
defer gz.Close()
_, err = gz.Write(b)
if err != nil {
log.Panic(err)
}
// Flush not quaranteed to flush, must close
// gz.Flush()
gz.Close()
// turn into chunked base64 string
var bb bytes.Buffer
b64 := base64.NewEncoder(base64.StdEncoding, &bb)
b64.Write(gzBuff.Bytes())
b64.Close()
// b64File += "\n"
chunk := make([]byte, 80)
for n, _ := bb.Read(chunk); n > 0; n, _ = bb.Read(chunk) {
b64File += string(chunk[0:n]) + "\n"
}
fPath = applyPathOptions(fPath)
fmt.Println("Processing:", fPath)
// write out here
writer.WriteString(fmt.Sprintf(dirFileStart, fPath, file.Name(), info.Size(), info.Mode(), info.ModTime().Unix(), false, b64File))
writer.WriteString(dirFileEndArray)
}
}
func applyPathOptions(path string) string {
path = strings.TrimPrefix(path, *flagPrefix)
path = strings.TrimLeft(path, string(os.PathSeparator))
return string(os.PathSeparator) + path
}