Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from Songmu/go-imports
support `meta name="go-import"` to detect Go repository
- Loading branch information
Showing
6 changed files
with
172 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
|
||
"golang.org/x/net/html" | ||
) | ||
|
||
func detectGoImport(u *url.URL) (string, *url.URL, error) { | ||
goGetU, _ := url.Parse(u.String()) // clone | ||
q := goGetU.Query() | ||
q.Add("go-get", "1") | ||
goGetU.RawQuery = q.Encode() | ||
|
||
cli := &http.Client{ | ||
CheckRedirect: func(req *http.Request, via []*http.Request) error { | ||
// never follow redirection | ||
return http.ErrUseLastResponse | ||
}, | ||
} | ||
req, _ := http.NewRequest(http.MethodGet, goGetU.String(), nil) | ||
req.Header.Add("User-Agent", fmt.Sprintf("ghq/%s (+https://github.com/motemen/ghq)", Version)) | ||
resp, err := cli.Do(req) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
return detectVCSAndRepoURL(resp.Body) | ||
} | ||
|
||
// find meta tag like following from thml | ||
// <meta name="go-import" content="gopkg.in/yaml.v2 git https://gopkg.in/yaml.v2"> | ||
// ref. https://golang.org/cmd/go/#hdr-Remote_import_paths | ||
func detectVCSAndRepoURL(r io.Reader) (string, *url.URL, error) { | ||
doc, err := html.Parse(r) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
|
||
var goImportContent string | ||
|
||
var f func(*html.Node) | ||
f = func(n *html.Node) { | ||
if goImportContent != "" { | ||
return | ||
} | ||
if n.Type == html.ElementNode && n.Data == "meta" { | ||
var ( | ||
goImportMeta = false | ||
content = "" | ||
) | ||
for _, a := range n.Attr { | ||
if a.Key == "name" && a.Val == "go-import" { | ||
goImportMeta = true | ||
continue | ||
} | ||
if a.Key == "content" { | ||
content = a.Val | ||
} | ||
} | ||
if goImportMeta && content != "" { | ||
goImportContent = content | ||
} | ||
} | ||
for c := n.FirstChild; c != nil; c = c.NextSibling { | ||
f(c) | ||
} | ||
} | ||
f(doc) | ||
|
||
stuffs := strings.Fields(goImportContent) | ||
if len(stuffs) < 3 { | ||
return "", nil, fmt.Errorf("no go-import meta tags detected") | ||
} | ||
u, err := url.Parse(stuffs[2]) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
return stuffs[1], u, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestDetectVCSAndRepoURL(t *testing.T) { | ||
input := `<html> | ||
<head> | ||
<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> | ||
<body> | ||
go get gopkg.in/yaml.v2 | ||
</body> | ||
</html>` | ||
|
||
vcs, u, err := detectVCSAndRepoURL(strings.NewReader(input)) | ||
if vcs != "git" { | ||
t.Errorf("want: %q, got: %q", "git", vcs) | ||
} | ||
expectedURL := "https://gopkg.in/yaml.v2" | ||
if u.String() != expectedURL { | ||
t.Errorf("want: %q, got: %q", expectedURL, u.String()) | ||
} | ||
if err != nil { | ||
t.Errorf("something went wrong: %s", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters