This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
271 lines (226 loc) · 6.86 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
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
// exists returns whether the given file or directory exists
func fileExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil { return true, nil }
if os.IsNotExist(err) { return false, nil }
return true, err
}
func killDeferred(process *os.Process, shutdownComplete *sync.WaitGroup) {
defer shutdownComplete.Done()
log.Println("shutting down godoc server.")
err := process.Kill()
if err != nil {
log.Panicf("error killing godoc server process: %v", err)
}
log.Println("go doc server shut down.")
}
func runDocServer(
settings *Settings,
shutdownSignal *sync.WaitGroup,
shutdownComplete *sync.WaitGroup,
) {
log.Println("starting up godoc server at", settings.ServerHost+".")
command := exec.Command("godoc", "-http="+settings.ServerHost)
if err := command.Start(); err != nil {
log.Panicf("error starting godoc server: %v", err)
}
defer killDeferred(command.Process, shutdownComplete)
shutdownSignal.Wait()
}
func scrapeModulePages(settings *Settings) {
pathRegex := regexp.QuoteMeta("/pkg/" + settings.ModName) + `|\.css|\.png|\.js`
wgetCommand := exec.Command(
"wget",
// save HTML/CSS documents with proper extensions
"-E",
// Convert links to local files
"-k",
// get all images, etc. needed to display HTML page
"-p",
// don't create directories
"-nd",
// specify recursive download
"-r",
// No maximum recursion depth
"-l", "50",
// don't ascend to the parent directory
"-np",
// accept regex
"--accept-regex", pathRegex,
// destination directory
"-P", settings.BuildDir,
// execute a `.wgetrc'-style command
"-erobots=off",
// root path to start crawl
settings.ServerHost+"/pkg/"+settings.ModName,
)
log.Println("wget command:", wgetCommand.Args)
output, err := wgetCommand.CombinedOutput()
if err != nil {
// check if the download worked at all
exists, err := fileExists(settings.BuildDir + "/style.css")
if !exists || err != nil {
log.Panicf(
"error scraping docs: %v, output: %v", err, string(output),
)
}
}
log.Print(
"\n\n##### WGET OUTPUT #####\n\n",
string(output),
"\n\n##### END OUTPUT #####\n\n",
)
}
func waitForServer(settings *Settings) {
timer := time.AfterFunc(10*time.Second, func() {
log.Panicf("timeout checking server.")
})
client := http.Client{Timeout: 1 * time.Second}
for true {
getPath := "http://" + settings.ServerHost + "/pkg/"
log.Println("Checking Server Status:", getPath)
resp, err := client.Get(getPath)
var responsePrint string
if err != nil {
responsePrint = err.Error()
} else {
responsePrint = resp.Status
}
log.Println("Response:", responsePrint)
if err != nil || resp == nil || resp.StatusCode != 200 {
time.Sleep(time.Second)
continue
} else {
timer.Stop()
break
}
}
}
func runServerAndScrapeDocs(settings *Settings) {
// We need to kill godoc if it is running.
_ = exec.Command("killall", "godoc").Run()
// Set up a shutdown event to signal to the goroutine running our docs server to
// kill that process.
shutDownSignal := sync.WaitGroup{}
shutDownSignal.Add(1)
shutDownComplete := sync.WaitGroup{}
shutDownComplete.Add(1)
// Defer sending a signal to shutdown the server and wait for it to shut down.
defer func() {
shutDownSignal.Done()
shutDownComplete.Wait()
}()
// Run the godoc server in a different goroutine.
go runDocServer(settings, &shutDownSignal, &shutDownComplete)
waitForServer(settings)
// Scrape all the documentation from the server.
scrapeModulePages(settings)
}
// Making the directory with os.MkDirAll can cause permissions errors that don't occur
// when making each directory individually.
func createBuildDir(path string) {
// current directory we want to make.
var makeDir string
// go through the directories individually.
for _, subdir := range strings.Split(path, "/") {
makeDir += "/" + subdir
makeDir = strings.TrimPrefix(makeDir, "/")
if err := os.Mkdir(makeDir, os.ModePerm); err != nil {
if strings.HasSuffix(err.Error(), "file exists") {
continue
}
log.Panicf("error creating build dir: %v", err)
}
}
}
// initialize the build directory
func setupBuildDir(settings *Settings) {
// Clear the build directory.
if err := os.RemoveAll(settings.BuildDir); err != nil {
log.Panicf("error removing build directory: %v", err)
}
createBuildDir(settings.BuildDir)
// We want to create a dummy index.html so that when we use wget, that name is
// reserved for our root file. We can't specify an output file when crawling so we
// need to reserve it.
if _, err := os.Create(settings.BuildDir + "/index.html"); err != nil {
log.Panicf("could not create dummy index: %v", err)
}
}
func renameEntryPoint(runInfo *RunInfo) (newPath string) {
settings := runInfo.Settings
stringSplit := strings.Split(settings.ModName, "/")
goDocBaseName := stringSplit[len(stringSplit)-1]
oldPath := settings.BuildDir + "/" + goDocBaseName + ".html"
newPath = settings.BuildDir + "/" + settings.HTMLBaseName + "-root.html"
err := os.Rename(oldPath, newPath)
if err != nil {
log.Panic("error while renaming entry file:", err)
}
runInfo.HtmlFiles = append(runInfo.HtmlFiles, newPath)
return newPath
}
func renameOutputFiles(runInfo *RunInfo) {
// make a mapping of the current files to what we want to rename them to.
settings := runInfo.Settings
entryPoint := renameEntryPoint(runInfo)
matches, err := filepath.Glob(settings.BuildDir + "/" + "*" + ".html")
if err != nil {
log.Panic("could not find result files:", err.Error())
}
for i, oldPath := range matches {
// skip the entry-point since we have already renamed it
if oldPath == entryPoint {
continue
}
index := i + 1
newPath := settings.HTMLBaseName + "." + strconv.Itoa(index) + ".html"
newPath = settings.BuildDir + "/" + newPath
err := os.Rename(oldPath, newPath)
if err != nil {
log.Panicf("error renaming %q to %q", oldPath, newPath)
}
runInfo.DocFileInfo = append(
runInfo.DocFileInfo, NewDocFileInfo(oldPath, newPath),
)
runInfo.HtmlFiles = append(runInfo.HtmlFiles, newPath)
}
}
// rewrites the internal links of the html files
func rewriteHTMLLinks(runInfo *RunInfo) {
for _, filePath := range runInfo.HtmlFiles {
for _, info := range runInfo.DocFileInfo {
data, err := ioutil.ReadFile(filePath)
if err != nil {
log.Panicf("error opening file '%v': %v", filePath, err)
}
data = info.HtmlReplaceRegex1.ReplaceAll(data, info.HtmlReplaceWith1)
data = info.HtmlReplaceRegex2.ReplaceAll(data, info.HtmlReplaceWith2)
err = ioutil.WriteFile(filePath, data, os.ModePerm)
if err != nil {
log.Panicf("error altering output file: %v", err)
}
}
}
}
func main() {
runInfo := setupRunInfo()
setupBuildDir(runInfo.Settings)
runServerAndScrapeDocs(runInfo.Settings)
renameOutputFiles(runInfo)
rewriteHTMLLinks(runInfo)
}