glow/gitlab.go

49 lines
971 B
Go
Raw Normal View History

2019-11-25 05:55:50 +00:00
package main
import (
"errors"
"net/http"
"net/url"
"strings"
)
2020-12-01 03:12:05 +00:00
// isGitLabURL tests a string to determine if it is a well-structured GitLab URL.
2019-11-25 05:55:50 +00:00
func isGitLabURL(s string) (string, bool) {
if strings.HasPrefix(s, "gitlab.com/") {
s = "https://" + s
}
u, err := url.ParseRequestURI(s)
if err != nil {
return "", false
}
return u.String(), strings.ToLower(u.Host) == "gitlab.com"
}
2020-12-01 03:12:05 +00:00
// findGitLabREADME tries to find the correct README filename in a repository.
2020-03-31 06:53:35 +00:00
func findGitLabREADME(s string) (*source, error) {
2019-11-25 05:55:50 +00:00
u, err := url.ParseRequestURI(s)
if err != nil {
return nil, err
}
for _, r := range readmeNames {
v := u
v.Path += "/raw/master/" + r
2022-10-25 14:40:51 +00:00
// nolint:bodyclose
// it is closed on the caller
2019-11-25 05:55:50 +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-25 05:55:50 +00:00
}
}
return nil, errors.New("can't find README in GitLab repository")
}