Skip to content

Commit

Permalink
Support to read GitHub token from env
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Jun 19, 2022
1 parent 2696934 commit a24b90d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 11 additions & 1 deletion function/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"html/template"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a24b90d

Please sign in to comment.