/
main.go
346 lines (305 loc) · 8.21 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
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"errors"
"flag"
"fmt"
"hash/crc32"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
var chgAttempts = changeAttempts{
changes: make(map[string]string),
errors: make(map[string]string),
}
func main() {
baseDir := flag.String("dir", "./", "specifies the directory to scan recursively in for html files")
flag.Parse()
AppendHashes(*baseDir)
chgAttempts.printChangesErrors()
}
type changeAttempts struct {
changes map[string]string // [fileName.html]changeAttempts
errors map[string]string // [fileName.html]changeAttempts
}
type Change struct {
fileNameFrom string
fileNameTo string
htmlFile string
}
func (c *changeAttempts) addChange(htmlFile, nameFrom, nameTo string) {
if _, exists := c.changes[htmlFile]; !exists {
c.changes[htmlFile] = "Changes for " + htmlFile + ":"
}
c.changes[htmlFile] = c.changes[htmlFile] + fmt.Sprintf("\n%s => %s", nameFrom, nameTo)
}
func (c *changeAttempts) addError(htmlFile, err string) {
if _, exists := c.errors[htmlFile]; !exists {
c.errors[htmlFile] = "Errors for " + htmlFile + ":"
}
c.errors[htmlFile] = c.errors[htmlFile] + fmt.Sprintf("\nERROR:%s", err)
}
func (c *changeAttempts) printChangesErrors() {
for htmlChg, changes := range c.changes {
thisErrs := ""
// grab the errors associated with the curr htmlChg
for htmlErr, errs := range c.errors {
if htmlChg == htmlErr {
thisErrs = errs
}
}
fmt.Println(changes)
fmt.Println(thisErrs)
}
}
func AppendHashes(baseDir string) []Change {
htmlFilePaths, err := htmlFilePaths(baseDir)
if err != nil {
log.Fatal(err)
return []Change{}
}
var editJobs = []*job{}
for _, filePath := range htmlFilePaths {
b, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}
addEditJobs(&editJobs, filePath, string(b))
}
changes := renameAll(editJobs)
return changes
}
func htmlFilePaths(baseDir string) ([]string, error) {
var htmlFilePaths []string
err := filepath.Walk(baseDir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
split := strings.Split(info.Name(), ".")
if len(split) > 0 {
ext := split[len(split)-1]
if ext == "html" || ext == "htm" {
htmlFilePaths = append(htmlFilePaths, path)
}
}
return nil
})
if err != nil {
return nil, nil
}
return htmlFilePaths, nil
}
type job struct {
fileNameWantToRename string
filePathWantToRename string
renameTo string
tagPath string
wholeTag string
htmlFile string
}
type renameJob struct {
pathFrom string
pathTo string
htmlFile string
}
func renameAll(jobs []*job) []Change {
for _, job := range jobs {
job.filePathWantToRename = filepath.Clean(job.filePathWantToRename)
}
renameJobs := make(map[renameJob]struct{})
for _, job := range jobs {
dir, _ := filepath.Split(job.filePathWantToRename)
renameJobs[renameJob{
pathFrom: job.filePathWantToRename,
pathTo: dir + job.renameTo,
htmlFile: job.htmlFile,
}] = struct{}{}
} // only want to rename a file once, else we may attempt to rename an already renamed file, and it won't exist
for job, _ := range renameJobs {
err := os.Rename(job.pathFrom, job.pathTo)
if err != nil {
chgAttempts.addError(job.htmlFile, err.Error())
}
}
// batch html edits
var changes []Change
for _, job := range jobs {
fileContent, err := ioutil.ReadFile(job.htmlFile)
if err != nil {
chgAttempts.addError(job.htmlFile, err.Error())
}
newFileContent := strings.ReplaceAll(string(fileContent), job.wholeTag, newTag(job))
err = ioutil.WriteFile(job.htmlFile, []byte(newFileContent), 0644)
if err != nil {
chgAttempts.addError(job.htmlFile, err.Error())
continue
}
changes = append(changes, Change{
fileNameFrom: job.filePathWantToRename,
fileNameTo: job.renameTo,
htmlFile: job.htmlFile,
})
chgAttempts.addChange(job.htmlFile, job.filePathWantToRename, job.renameTo)
}
return changes
}
func newTag(j *job) string {
newTag := strings.ReplaceAll(j.wholeTag, j.tagPath+j.fileNameWantToRename, j.tagPath+j.renameTo)
return strings.ReplaceAll(j.wholeTag, j.wholeTag, newTag)
}
type tagInfo struct {
tagType string
wholeTag string
startTag int
}
func addEditJobs(jobs *[]*job, htmlFilePath, fileContent string) {
dir, _ := filepath.Split(htmlFilePath)
tags := tagsFromHTML(fileContent)
for _, ti := range tags {
if ti.tagType == "script" {
src, err := srcFilePath(ti.wholeTag)
if err != nil && err.Error() == "src is empty" {
return // normal for script tags to not have srcs
}
if err != nil {
chgAttempts.addError(htmlFilePath, err.Error())
return
}
addJob(jobs, dir, src, htmlFilePath, ti.wholeTag)
}
if ti.tagType == "link" {
href, err := hrefFilePath(ti.wholeTag)
if err != nil {
chgAttempts.addError(htmlFilePath, err.Error())
return
}
addJob(jobs, dir, href, htmlFilePath, ti.wholeTag)
}
}
}
func addJob(jobs *[]*job, dir string, srcHref string, htmlFilePath string, wholeTag string) {
hashedFileName, err := getHashedFileName(dir + srcHref) // change to rename file
if err != nil {
chgAttempts.addError(htmlFilePath, err.Error())
return
}
_, originalName := filepath.Split(dir + srcHref)
tagLocalPath, _ := filepath.Split(srcHref)
*jobs = append(*jobs, &job{
fileNameWantToRename: originalName,
filePathWantToRename: dir + srcHref,
renameTo: hashedFileName,
tagPath: tagLocalPath,
htmlFile: htmlFilePath,
wholeTag: wholeTag,
})
}
// Renames file at filePath with a cache clobber certified hash.
// Returns a newly renamed filepath.
// Will remove the previous cc hash if it exists.
func getHashedFileName(filePath string) (string, error) {
b, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
ccHash := "cc" + fmt.Sprint(hash(string(b))) // cc for CACHE CLOBBER
_, fileName := filepath.Split(filePath)
splitAtDash := strings.Split(fileName, "-")
hashAndExt := splitAtDash[len(splitAtDash)-1]
splitAtDot := strings.Split(hashAndExt, ".")
possibleHash := splitAtDot[len(splitAtDot)-2]
if isCCHash(possibleHash) {
newFileName := strings.Replace(fileName, possibleHash, ccHash, 1)
return newFileName, nil
}
ext := filepath.Ext(filePath)
newFileName := fileName[:len(fileName)-len(ext)] + "-" + ccHash + ext
return newFileName, nil
}
func isCCHash(s string) bool {
if len(s) < 3 { //"cc#" is minimum
return false
}
if s[:2] != "cc" {
return false
}
return true
}
func hash(s string) uint32 {
hasher := crc32.New(crc32.IEEETable)
_, err := io.WriteString(hasher, s)
if err != nil {
fmt.Println(err)
}
return hasher.Sum32()
}
// Runs func op for each tag found in fileContent.
func tagsFromHTML(fileContent string) []tagInfo {
tags := make([]tagInfo, 0, 0)
insideTag := false
readTagType := false // read = past tense
startTag := 0
currTagType := ""
for i, char := range fileContent {
if char == '<' {
startTag = i
insideTag = true
continue
}
if char == '>' {
wholeTag := fileContent[startTag : i+1]
tags = append(tags, tagInfo{
tagType: currTagType,
wholeTag: wholeTag,
startTag: startTag,
})
currTagType = ""
readTagType = false
insideTag = false
continue
}
if insideTag {
if char == ' ' && !readTagType {
currTagType = fileContent[startTag+1 : i] // startTag+1 cuts off the <
readTagType = true
}
if !readTagType {
currTagType = currTagType + string(char)
}
}
}
return tags
}
func hrefFilePath(wholeTag string) (string, error) {
r, err := regexp.Compile(`(href=".+")`)
if err != nil {
log.Fatal(err)
}
filePath := string(r.Find([]byte(wholeTag)))
if filePath == "" {
return "", errors.New("href is empty")
}
if !strings.Contains(filePath, ".css") {
return "", errors.New("href is not css file")
}
return filePath[6 : len(filePath)-1], nil // cuts off src=" and "
}
func srcFilePath(wholeTag string) (string, error) {
r, err := regexp.Compile(`(src=".+")`)
if err != nil {
log.Fatal(err)
}
filePath := string(r.Find([]byte(wholeTag)))
if filePath == "" {
return "", errors.New("src is empty")
}
if !strings.Contains(filePath, ".js") {
return "", errors.New("src is not js file")
}
return filePath[5 : len(filePath)-1], nil // cuts off src=" and "
}