/
main.go
72 lines (67 loc) · 1.49 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
package main
import (
"bufio"
"context"
"flag"
"net/http"
"os"
"os/exec"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
func main() {
var outputDir string
var httpAddr string
var help bool
flag.StringVar(&outputDir, "output", "docs", "output directory")
flag.StringVar(&httpAddr, "http", "localhost:6060", "godoc server listen on")
flag.BoolVar(&help, "help", false, "help")
flag.Parse()
if help {
flag.Usage()
return
}
file, err := os.Open("go.mod")
if err != nil {
log.Errorln("open go.mod failed")
return
}
defer file.Close()
rd := bufio.NewReader(file)
_, err = rd.ReadString(' ')
if err != nil {
log.Errorln("read go.mod failed")
return
}
modName, err := rd.ReadString('\n')
if err != nil {
log.Errorln("read go.mod failed")
return
}
modName = strings.TrimSpace(modName)
log.Infoln("mod name: ", modName)
ctx, cancel := context.WithCancel(context.Background())
defer func() {
cancel()
<-ctx.Done()
log.Infoln("godoc stopped")
}()
go func() {
godocCmd := exec.CommandContext(ctx, "godoc", "-http", httpAddr)
log.Infoln("run godoc:", godocCmd.String())
godocCmd.Run()
}()
log.Infoln("ping godoc")
for {
resp, err := http.Get("http://" + httpAddr)
if err == nil && resp.StatusCode == http.StatusOK {
break
}
time.Sleep(300 * time.Millisecond)
}
log.Infoln("run wget")
wgetCmd := exec.Command("wget", "-P", outputDir, "-r", "-np", "-N", "-E", "-p", "-k", "http://"+httpAddr+"/pkg/"+modName)
wgetCmd.Run()
log.Infoln("wget stopped")
}