fix: check other possible readme paths/branches (#450)

* fix: check other possible readme paths/branches

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: url

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: Readme.md

---------

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker 2024-07-03 13:09:20 -03:00 committed by GitHub
parent f3d0cb89da
commit 8e51396575
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 46 additions and 38 deletions

View file

@ -2,6 +2,7 @@ package main
import ( import (
"errors" "errors"
"fmt"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -29,19 +30,21 @@ func findGitHubREADME(s string) (*source, error) {
} }
u.Host = "raw.githubusercontent.com" u.Host = "raw.githubusercontent.com"
for _, r := range readmeNames { for _, b := range readmeBranches {
v := u for _, r := range readmeNames {
v.Path += "/master/" + r v := *u
v.Path += fmt.Sprintf("/%s/%s", b, r)
// nolint:bodyclose // nolint:bodyclose
// it is closed on the caller // it is closed on the caller
resp, err := http.Get(v.String()) resp, err := http.Get(v.String())
if err != nil { if err != nil {
return nil, err return nil, err
} }
if resp.StatusCode == http.StatusOK { if resp.StatusCode == http.StatusOK {
return &source{resp.Body, v.String()}, nil return &source{resp.Body, v.String()}, nil
}
} }
} }

View file

@ -2,6 +2,7 @@ package main
import ( import (
"errors" "errors"
"fmt"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -28,19 +29,21 @@ func findGitLabREADME(s string) (*source, error) {
return nil, err return nil, err
} }
for _, r := range readmeNames { for _, b := range readmeBranches {
v := u for _, r := range readmeNames {
v.Path += "/raw/master/" + r v := *u
v.Path += fmt.Sprintf("/raw/%s/%s", b, r)
// nolint:bodyclose // nolint:bodyclose
// it is closed on the caller // it is closed on the caller
resp, err := http.Get(v.String()) resp, err := http.Get(v.String())
if err != nil { if err != nil {
return nil, err return nil, err
} }
if resp.StatusCode == http.StatusOK { if resp.StatusCode == http.StatusOK {
return &source{resp.Body, v.String()}, nil return &source{resp.Body, v.String()}, nil
}
} }
} }

View file

@ -14,15 +14,16 @@ func TestGlowSources(t *testing.T) {
} }
for _, v := range tt { for _, v := range tt {
buf := &bytes.Buffer{} t.Run(v, func(t *testing.T) {
err := executeArg(rootCmd, v, buf) buf := &bytes.Buffer{}
err := executeArg(rootCmd, v, buf)
if err != nil { if err != nil {
t.Errorf("Error during execution (args: %s): %v", v, err) t.Errorf("Error during execution (args: %s): %v", v, err)
} }
if buf.Len() == 0 { if buf.Len() == 0 {
t.Errorf("Output buffer should not be empty (args: %s)", v) t.Errorf("Output buffer should not be empty (args: %s)", v)
} }
})
} }
} }

15
main.go
View file

@ -28,13 +28,14 @@ var (
// CommitSHA as provided by goreleaser. // CommitSHA as provided by goreleaser.
CommitSHA = "" CommitSHA = ""
readmeNames = []string{"README.md", "README"} readmeNames = []string{"README.md", "README", "Readme.md", "Readme", "readme.md", "readme"}
configFile string readmeBranches = []string{"main", "master"}
pager bool configFile string
style string pager bool
width uint style string
showAllFiles bool width uint
mouse bool showAllFiles bool
mouse bool
rootCmd = &cobra.Command{ rootCmd = &cobra.Command{
Use: "glow [SOURCE|DIR]", Use: "glow [SOURCE|DIR]",