2019-11-22 02:52:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-12-01 03:12:05 +00:00
|
|
|
// isGitHubURL tests a string to determine if it is a well-structured GitHub URL.
|
2019-11-25 03:55:09 +00:00
|
|
|
func isGitHubURL(s string) (string, bool) {
|
|
|
|
if strings.HasPrefix(s, "github.com/") {
|
|
|
|
s = "https://" + s
|
|
|
|
}
|
|
|
|
|
2019-11-22 02:52:21 +00:00
|
|
|
u, err := url.ParseRequestURI(s)
|
|
|
|
if err != nil {
|
2019-11-25 03:55:09 +00:00
|
|
|
return "", false
|
2019-11-22 02:52:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 03:55:09 +00:00
|
|
|
return u.String(), strings.ToLower(u.Host) == "github.com"
|
2019-11-22 02:52:21 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 03:12:05 +00:00
|
|
|
// findGitHubREADME tries to find the correct README filename in a repository.
|
2020-03-31 06:53:35 +00:00
|
|
|
func findGitHubREADME(s string) (*source, error) {
|
2019-11-22 02:52:21 +00:00
|
|
|
u, err := url.ParseRequestURI(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
u.Host = "raw.githubusercontent.com"
|
|
|
|
|
|
|
|
for _, r := range readmeNames {
|
|
|
|
v := u
|
|
|
|
v.Path += "/master/" + r
|
|
|
|
|
2022-10-25 14:40:51 +00:00
|
|
|
// nolint:bodyclose
|
|
|
|
// it is closed on the caller
|
2019-11-22 02:52:21 +00:00
|
|
|
resp, err := http.Get(v.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
2020-03-31 06:53:35 +00:00
|
|
|
return &source{resp.Body, v.String()}, nil
|
2019-11-22 02:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("can't find README in GitHub repository")
|
|
|
|
}
|