diff --git a/function/github.go b/function/github.go index e653322..fe4f402 100644 --- a/function/github.go +++ b/function/github.go @@ -7,6 +7,7 @@ import ( "html/template" "io/ioutil" "net/http" + "os" "regexp" "strings" ) @@ -41,9 +42,18 @@ func PrintContributors(owner, repo string) (output string) { func ghRequest(api string) (data []byte, err error) { var ( resp *http.Response + req *http.Request ) - if resp, err = http.Get(api); err == nil && resp.StatusCode == http.StatusOK { + if req, err = http.NewRequest(http.MethodGet, api, nil); err != nil { + return + } + + if os.Getenv("GITHUB_TOKEN") != "" { + req.Header.Set("Authorization", fmt.Sprintf("token %s", os.Getenv("GITHUB_TOKEN"))) + } + + if resp, err = http.DefaultClient.Do(req); err == nil && resp.StatusCode == http.StatusOK { data, err = ioutil.ReadAll(resp.Body) } return diff --git a/main.go b/main.go index 2a2a56f..dc8c2a2 100644 --- a/main.go +++ b/main.go @@ -120,7 +120,7 @@ func loadTemplate(templateFile string, includeHeader bool) (readmeTpl string, er } func (o *option) runE(cmd *cobra.Command, args []string) (err error) { - logger = log.New(cmd.OutOrStdout(), "", log.LstdFlags) + logger = log.New(cmd.ErrOrStderr(), "", log.LstdFlags) if o.printFunctions { printFunctions(cmd.OutOrStdout()) return @@ -298,7 +298,9 @@ func newRootCommand() (cmd *cobra.Command) { cmd = &cobra.Command{ Use: "yaml-readme", Short: "A helper to generate a README file from Golang-based template", - RunE: opt.runE, + Long: `A helper to generate a README file from Golang-based template +Some functions rely on the GitHub API, in order to avoid X-RateLimit-Limit errors you can set an environment variable: 'GITHUB_TOKEN'`, + RunE: opt.runE, } cmd.SetOut(os.Stdout) flags := cmd.Flags()