-
Notifications
You must be signed in to change notification settings - Fork 11
/
goimport.go
120 lines (101 loc) · 2.94 KB
/
goimport.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
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package golang
import (
"errors"
"fmt"
"net/http"
"strings"
"knative.dev/toolbox/pkg/git"
"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 (m *MetaImport) OrgRepo() (string, string) {
repoRoot := strings.TrimSuffix(m.RepoRoot, ".git")
urlParts := strings.Split(repoRoot, "://")
parts := strings.Split(urlParts[len(urlParts)-1], "/")
if len(parts) >= 2 {
return parts[len(parts)-2], parts[len(parts)-1]
}
panic("unknown repo root: " + m.RepoRoot)
}
func metaContent(doc *html.Node, name string) (string, error) {
var meta *html.Node
var crawler func(*html.Node)
crawler = func(node *html.Node) {
if node.Type == html.ElementNode && node.Data == "meta" {
for _, attr := range node.Attr {
if attr.Key == "name" && attr.Val == name {
meta = node
return
}
}
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
crawler(child)
}
}
crawler(doc)
if meta != nil {
for _, attr := range meta.Attr {
if attr.Key == "content" {
return attr.Val, nil
}
}
}
return "", fmt.Errorf("missing <meta name=%s> in the node tree", name)
}
// GetMetaImport fetches and parses header tags named go-import into a
// MetaImport object.
func GetMetaImport(url string) (*MetaImport, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
doc, err := html.Parse(resp.Body)
if err != nil {
return nil, err
}
content, err := metaContent(doc, "go-import")
if err != nil {
return nil, err
}
f := strings.Fields(content)
return &MetaImport{
Prefix: f[0],
VCS: f[1],
RepoRoot: f[2],
}, nil
}
// ModuleToRepo resolves a go module name to a remote git repo.
func ModuleToRepo(module string) (*git.Repo, error) {
// If module has more than 1 domain + 2 paths (e.g. github.com/blang/semver/v4),
// make it 1 domain + 2 paths. Otherwise, it fails to fetch go import on GetMetaImport().
m := strings.Split(module, "/")
if len(m) > 2 {
module = strings.Join(m[0:3], "/")
}
url := fmt.Sprintf("https://%s?go-get=1", module)
meta, err := GetMetaImport(url)
if err != nil {
return nil, fmt.Errorf("unable to fetch go import %s: %w", url, err)
}
if meta.VCS != "git" {
return nil, errors.New("unknown VCS: " + meta.VCS)
}
return git.GetRepo(module, meta.RepoRoot)
}