mirror of
https://github.com/charmbracelet/glow
synced 2024-12-14 06:02:27 +00:00
Basic GitLab support
This commit is contained in:
parent
240eb4f2f8
commit
9ffa4d5b16
2 changed files with 53 additions and 0 deletions
46
gitlab.go
Normal file
46
gitlab.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// isGitLabURL tests a string to determine if it is a well-structured GitLab URL
|
||||
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"
|
||||
}
|
||||
|
||||
// findGitLabREADME tries to find the correct README filename in a repository
|
||||
func findGitLabREADME(s string) (*http.Response, error) {
|
||||
u, err := url.ParseRequestURI(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, r := range readmeNames {
|
||||
v := u
|
||||
v.Path += "/raw/master/" + r
|
||||
|
||||
resp, err := http.Get(v.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return resp, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errors.New("can't find README in GitLab repository")
|
||||
}
|
7
main.go
7
main.go
|
@ -43,6 +43,13 @@ func readerFromArg(s string) (io.ReadCloser, error) {
|
|||
}
|
||||
return resp.Body, nil
|
||||
}
|
||||
if u, ok := isGitLabURL(s); ok {
|
||||
resp, err := findGitLabREADME(u)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Body, nil
|
||||
}
|
||||
|
||||
if u, err := url.ParseRequestURI(s); err == nil {
|
||||
if u.Scheme != "http" && u.Scheme != "https" {
|
||||
|
|
Loading…
Reference in a new issue