From ec9cc0bde48b6a32f6d390c01132a3fcbcb950ca Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Tue, 31 Mar 2020 08:53:35 +0200 Subject: [PATCH] Make source private --- github.go | 4 ++-- gitlab.go | 4 ++-- main.go | 20 +++++++++++++------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/github.go b/github.go index 1b04da7..3b944cc 100644 --- a/github.go +++ b/github.go @@ -22,7 +22,7 @@ func isGitHubURL(s string) (string, bool) { } // findGitHubREADME tries to find the correct README filename in a repository -func findGitHubREADME(s string) (*Source, error) { +func findGitHubREADME(s string) (*source, error) { u, err := url.ParseRequestURI(s) if err != nil { return nil, err @@ -39,7 +39,7 @@ func findGitHubREADME(s string) (*Source, error) { } if resp.StatusCode == http.StatusOK { - return &Source{resp.Body, v.String()}, nil + return &source{resp.Body, v.String()}, nil } } diff --git a/gitlab.go b/gitlab.go index 7c6dfa5..7bebc93 100644 --- a/gitlab.go +++ b/gitlab.go @@ -22,7 +22,7 @@ func isGitLabURL(s string) (string, bool) { } // findGitLabREADME tries to find the correct README filename in a repository -func findGitLabREADME(s string) (*Source, error) { +func findGitLabREADME(s string) (*source, error) { u, err := url.ParseRequestURI(s) if err != nil { return nil, err @@ -38,7 +38,7 @@ func findGitLabREADME(s string) (*Source, error) { } if resp.StatusCode == http.StatusOK { - return &Source{resp.Body, v.String()}, nil + return &source{resp.Body, v.String()}, nil } } diff --git a/main.go b/main.go index 0d5a9ab..80a1b39 100644 --- a/main.go +++ b/main.go @@ -36,14 +36,16 @@ var ( } ) -type Source struct { +// source provides a readable markdown source +type source struct { reader io.ReadCloser URL string } -func readerFromArg(s string) (*Source, error) { +func readerFromArg(s string) (*source, error) { + // from stdin if s == "-" { - return &Source{reader: os.Stdin}, nil + return &source{reader: os.Stdin}, nil } // a GitHub or GitLab URL (even without the protocol): @@ -76,7 +78,7 @@ func readerFromArg(s string) (*Source, error) { if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("HTTP status %d", resp.StatusCode) } - return &Source{resp.Body, u.String()}, nil + return &source{resp.Body, u.String()}, nil } } @@ -87,7 +89,7 @@ func readerFromArg(s string) (*Source, error) { } st, err := os.Stat(s) if err == nil && st.IsDir() { - var src *Source + var src *source _ = filepath.Walk(s, func(path string, info os.FileInfo, err error) error { for _, v := range readmeNames { if strings.EqualFold(filepath.Base(path), v) { @@ -97,12 +99,15 @@ func readerFromArg(s string) (*Source, error) { } u, _ := filepath.Abs(path) - src = &Source{r, u} + src = &source{r, u} + + // abort filepath.Walk return errors.New("source found") } } return nil }) + if src != nil { return src, nil } @@ -113,7 +118,7 @@ func readerFromArg(s string) (*Source, error) { // a file: r, err := os.Open(s) u, _ := filepath.Abs(s) - return &Source{r, u}, err + return &source{r, u}, err } func execute(cmd *cobra.Command, args []string) error { @@ -156,6 +161,7 @@ func executeArg(cmd *cobra.Command, arg string, w io.Writer) error { baseURL = u.String() + "/" } + // initialize glamour var gs glamour.TermRendererOption if style == "auto" { gs = glamour.WithAutoStyle()