skip go-import mod

This commit is contained in:
Songmu 2019-05-12 15:14:56 +09:00
parent 249d74beb1
commit 3ec364a2f1
2 changed files with 18 additions and 8 deletions

View file

@ -10,6 +10,12 @@ import (
"golang.org/x/net/html"
)
// metaImport represents the parsed <meta name="go-import"
// content="prefix vcs reporoot" /> tags from HTML files.
type metaImport struct {
Prefix, VCS, RepoRoot string
}
func detectGoImport(u *url.URL) (string, *url.URL, error) {
goGetU, _ := url.Parse(u.String()) // clone
q := goGetU.Query()
@ -42,11 +48,11 @@ func detectVCSAndRepoURL(r io.Reader) (string, *url.URL, error) {
return "", nil, err
}
var goImportContent string
var mImport *metaImport
var f func(*html.Node)
f = func(n *html.Node) {
if goImportContent != "" {
if mImport != nil {
return
}
if n.Type == html.ElementNode && n.Data == "meta" {
@ -63,8 +69,12 @@ func detectVCSAndRepoURL(r io.Reader) (string, *url.URL, error) {
content = a.Val
}
}
if goImportMeta && content != "" {
goImportContent = content
if f := strings.Fields(content); goImportMeta && len(f) == 3 && f[1] != "mod" {
mImport = &metaImport{
Prefix: f[0],
VCS: f[1],
RepoRoot: f[2],
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
@ -73,13 +83,12 @@ func detectVCSAndRepoURL(r io.Reader) (string, *url.URL, error) {
}
f(doc)
stuffs := strings.Fields(goImportContent)
if len(stuffs) < 3 {
if mImport == nil {
return "", nil, fmt.Errorf("no go-import meta tags detected")
}
u, err := url.Parse(stuffs[2])
u, err := url.Parse(mImport.RepoRoot)
if err != nil {
return "", nil, err
}
return stuffs[1], u, nil
return mImport.VCS, u, nil
}

View file

@ -8,6 +8,7 @@ import (
func TestDetectVCSAndRepoURL(t *testing.T) {
input := `<html>
<head>
<meta name="go-import" content="gopkg.in/yaml.v2 mod https://gopkg.in/yaml.v2">
<meta name="go-import" content="gopkg.in/yaml.v2 git https://gopkg.in/yaml.v2">
<meta name="go-source" content="gopkg.in/yaml.v2 _ https://github.com/go-yaml/yaml/tree/v2.2.2{/dir} https://github.com/go-yaml/yaml/blob/v2.2.2{/dir}/{file}#L{line}">
</head>