forked from davecheney/godoc2md
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imports.go
290 lines (273 loc) · 4.93 KB
/
imports.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
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/godoc"
)
// $ go list std
// Packages within /internal and /vendor have been omitted from this list.
// This is up-to-date as of Go 1.7.
var goListStd = `archive/tar
archive/zip
bufio
bytes
compress/bzip2
compress/flate
compress/gzip
compress/lzw
compress/zlib
container/heap
container/list
container/ring
context
crypto
crypto/aes
crypto/cipher
crypto/des
crypto/dsa
crypto/ecdsa
crypto/elliptic
crypto/hmac
crypto/md5
crypto/rand
crypto/rc4
crypto/rsa
crypto/sha1
crypto/sha256
crypto/sha512
crypto/subtle
crypto/tls
crypto/x509
crypto/x509/pkix
database/sql
database/sql/driver
debug/dwarf
debug/elf
debug/gosym
debug/macho
debug/pe
debug/plan9obj
encoding
encoding/ascii85
encoding/asn1
encoding/base32
encoding/base64
encoding/binary
encoding/csv
encoding/gob
encoding/hex
encoding/json
encoding/pem
encoding/xml
errors
expvar
flag
fmt
go/ast
go/build
go/constant
go/doc
go/format
go/importer
go/internal/gccgoimporter
go/internal/gcimporter
go/parser
go/printer
go/scanner
go/token
go/types
hash
hash/adler32
hash/crc32
hash/crc64
hash/fnv
html
html/template
image
image/color
image/color/palette
image/draw
image/gif
image/internal/imageutil
image/jpeg
image/png
index/suffixarray
io
io/ioutil
log
log/syslog
math
math/big
math/cmplx
math/rand
mime
mime/multipart
mime/quotedprintable
net
net/http
net/http/cgi
net/http/cookiejar
net/http/fcgi
net/http/httptest
net/http/httptrace
net/http/httputil
net/http/internal
net/http/pprof
net/internal/socktest
net/mail
net/rpc
net/rpc/jsonrpc
net/smtp
net/textproto
net/url
os
os/exec
os/signal
os/user
path
path/filepath
reflect
regexp
regexp/syntax
runtime
runtime/cgo
runtime/debug
runtime/internal/atomic
runtime/internal/sys
runtime/pprof
runtime/race
runtime/trace
sort
strconv
strings
sync
sync/atomic
syscall
testing
testing/iotest
testing/quick
text/scanner
text/tabwriter
text/template
text/template/parse
time
unicode
unicode/utf16
unicode/utf8
unsafe
`
func getStdLib() map[string]bool {
importPaths := make(map[string]bool)
envList := os.Getenv("GO_LIST_STD")
if envList != "" {
for _, importPath := range strings.Split(envList, "\n") {
if importPath != "" {
importPaths[importPath] = true
}
}
return importPaths
}
for _, importPath := range strings.Split(goListStd, "\n") {
if importPath != "" {
importPaths[importPath] = true
}
}
return importPaths
}
func importAsFunc(importPath string) string {
if *importAs != "" {
return *importAs
}
return importPath
}
func listImportsFunc(info *godoc.PageInfo, imports []string, importPath string) string {
var repoOwnerAndName string
if strings.HasPrefix(importPath, "github.com") {
repoOwnerAndName = strings.Join(strings.Split(importPath[len("github.com"):], "/")[:2], "")
}
var nonStd []string
for _, imp := range imports {
if !stdLib[imp] {
nonStd = append(nonStd, imp)
}
}
if len(nonStd) == 0 {
return "No packages beyond the Go standard library are imported."
}
if !*importLinks || repoOwnerAndName == "" {
return "- " + strings.Join(nonStd, "\n- ")
}
for i, imp := range nonStd {
if strings.HasPrefix(imp, "github.com/"+repoOwnerAndName) {
rel, err := filepath.Rel(importPath, imp)
if err != nil {
continue
}
nonStd[i] = fmt.Sprintf("[%s](./%s)", imp, rel)
continue
}
godocUrl := "https://godoc.org/" + imp
isVendored := isImportVendored(imp)
if !*verifyImportLinks {
if !isVendored {
nonStd[i] = fmt.Sprintf("[%s](%s)", imp, godocUrl)
continue
} else {
nonStd[i] = fmt.Sprintf("[%s](%s) ([godoc](%s))", imp, "/vendor/"+imp, godocUrl)
continue
}
} else {
hasGodoc := isValidGodocUrl(godocUrl, importLinksState)
if isVendored && hasGodoc {
nonStd[i] = fmt.Sprintf("[%s](%s) ([godoc](%s))", imp, "/vendor/"+imp, godocUrl)
continue
}
if isVendored {
nonStd[i] = fmt.Sprintf("[%s](%s)", imp, "/vendor/"+imp)
continue
}
if hasGodoc {
nonStd[i] = fmt.Sprintf("[%s](%s)", imp, godocUrl)
}
}
}
return "- " + strings.Join(nonStd, "\n- ")
}
func isValidGodocUrl(url string, validityMap map[string]string) bool {
if validityMap != nil && validityMap[url] != "" {
return validityMap[url] == validImportKey
}
resp, err := http.Head(url)
resp.Body.Close()
var validity string
if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 300 {
validity = validImportKey
} else {
validity = invalidImportKey
}
validityMap[url] = validity
if *importLinksFile != "" {
f, err := os.OpenFile(*importLinksFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return validity == validImportKey
}
defer f.Close()
_, err = f.WriteString(fmt.Sprintf("%s %s\n", url, validity))
if err != nil {
panic(err)
}
}
return validity == validImportKey
}
func isImportVendored(importPath string) bool {
if *vendorPath == "" {
return false
}
if _, err := os.Stat(filepath.Join(*vendorPath, importPath)); err != nil {
return false
}
return true
}