Support github URLs without protocol

This commit is contained in:
Christian Muehlhaeuser 2019-11-25 04:55:09 +01:00
commit 1baefbbec1
2 changed files with 15 additions and 11 deletions

View file

@ -8,13 +8,17 @@ import (
)
// isGitHubURL tests a string to determine if it is a well-structured GitHub URL
func isGitHubURL(s string) bool {
u, err := url.ParseRequestURI(s)
if err != nil {
return false
func isGitHubURL(s string) (string, bool) {
if strings.HasPrefix(s, "github.com/") {
s = "https://" + s
}
return strings.ToLower(u.Host) == "github.com"
u, err := url.ParseRequestURI(s)
if err != nil {
return "", false
}
return u.String(), strings.ToLower(u.Host) == "github.com"
}
// findGitHubREADME tries to find the correct README filename in a repository

16
main.go
View file

@ -34,19 +34,19 @@ func readerFromArg(s string) (io.ReadCloser, error) {
return os.Stdin, nil
}
if u, ok := isGitHubURL(s); ok {
resp, err := findGitHubREADME(u)
if err != nil {
return nil, err
}
return resp.Body, nil
}
if u, err := url.ParseRequestURI(s); err == nil {
if !strings.HasPrefix(u.Scheme, "http") {
return nil, fmt.Errorf("%s is not a supported protocol", u.Scheme)
}
if isGitHubURL(s) {
resp, err := findGitHubREADME(s)
if err != nil {
return nil, err
}
return resp.Body, nil
}
resp, err := http.Get(u.String())
if err != nil {
return nil, err