glow/github.go

50 lines
1,005 B
Go
Raw Permalink Normal View History

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
}
u, err := url.ParseRequestURI(s)
if err != nil {
2019-11-25 03:55:09 +00:00
return "", false
}
2019-11-25 03:55:09 +00:00
return u.String(), strings.ToLower(u.Host) == "github.com"
}
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) {
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
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
}
}
return nil, errors.New("can't find README in GitHub repository")
}