From 8e5139657547918b8e83c544cf68ec1e7e87b7c4 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 3 Jul 2024 13:09:20 -0300 Subject: [PATCH] fix: check other possible readme paths/branches (#450) * fix: check other possible readme paths/branches Signed-off-by: Carlos A Becker * fix: url Signed-off-by: Carlos A Becker * fix: Readme.md --------- Signed-off-by: Carlos A Becker --- github.go | 25 ++++++++++++++----------- gitlab.go | 25 ++++++++++++++----------- glow_test.go | 19 ++++++++++--------- main.go | 15 ++++++++------- 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/github.go b/github.go index b824cf5..ac64096 100644 --- a/github.go +++ b/github.go @@ -2,6 +2,7 @@ package main import ( "errors" + "fmt" "net/http" "net/url" "strings" @@ -29,19 +30,21 @@ func findGitHubREADME(s string) (*source, error) { } u.Host = "raw.githubusercontent.com" - for _, r := range readmeNames { - v := u - v.Path += "/master/" + r + for _, b := range readmeBranches { + for _, r := range readmeNames { + v := *u + v.Path += fmt.Sprintf("/%s/%s", b, r) - // nolint:bodyclose - // it is closed on the caller - resp, err := http.Get(v.String()) - if err != nil { - return nil, err - } + // 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 { - return &source{resp.Body, v.String()}, nil + if resp.StatusCode == http.StatusOK { + return &source{resp.Body, v.String()}, nil + } } } diff --git a/gitlab.go b/gitlab.go index e8c8a57..47059bb 100644 --- a/gitlab.go +++ b/gitlab.go @@ -2,6 +2,7 @@ package main import ( "errors" + "fmt" "net/http" "net/url" "strings" @@ -28,19 +29,21 @@ func findGitLabREADME(s string) (*source, error) { return nil, err } - for _, r := range readmeNames { - v := u - v.Path += "/raw/master/" + r + for _, b := range readmeBranches { + for _, r := range readmeNames { + v := *u + v.Path += fmt.Sprintf("/raw/%s/%s", b, r) - // nolint:bodyclose - // it is closed on the caller - resp, err := http.Get(v.String()) - if err != nil { - return nil, err - } + // 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 { - return &source{resp.Body, v.String()}, nil + if resp.StatusCode == http.StatusOK { + return &source{resp.Body, v.String()}, nil + } } } diff --git a/glow_test.go b/glow_test.go index ed4f0cd..37f4079 100644 --- a/glow_test.go +++ b/glow_test.go @@ -14,15 +14,16 @@ func TestGlowSources(t *testing.T) { } for _, v := range tt { - buf := &bytes.Buffer{} - err := executeArg(rootCmd, v, buf) - - if err != nil { - t.Errorf("Error during execution (args: %s): %v", v, err) - } - if buf.Len() == 0 { - t.Errorf("Output buffer should not be empty (args: %s)", v) - } + t.Run(v, func(t *testing.T) { + buf := &bytes.Buffer{} + err := executeArg(rootCmd, v, buf) + if err != nil { + t.Errorf("Error during execution (args: %s): %v", v, err) + } + if buf.Len() == 0 { + t.Errorf("Output buffer should not be empty (args: %s)", v) + } + }) } } diff --git a/main.go b/main.go index 4aac4e7..6ca2eea 100644 --- a/main.go +++ b/main.go @@ -28,13 +28,14 @@ var ( // CommitSHA as provided by goreleaser. CommitSHA = "" - readmeNames = []string{"README.md", "README"} - configFile string - pager bool - style string - width uint - showAllFiles bool - mouse bool + readmeNames = []string{"README.md", "README", "Readme.md", "Readme", "readme.md", "readme"} + readmeBranches = []string{"main", "master"} + configFile string + pager bool + style string + width uint + showAllFiles bool + mouse bool rootCmd = &cobra.Command{ Use: "glow [SOURCE|DIR]",