Use GITHUB_TOKEN to access GitHub and avoid access limit

Attempt to get GITHUB_TOKEN from environment variables
This commit is contained in:
2026-04-14 16:16:05 +08:00
parent c3045d0cfe
commit 69f90f015e

39
main.go
View File

@@ -1,6 +1,7 @@
package main
import (
"net/http"
"os"
"github.com/google/go-github/v60/github"
@@ -11,6 +12,17 @@ var (
gitHub *github.Client
)
// TokenTransport implements http.RoundTripper for Bearer token authentication
type TokenTransport struct {
Token string
Transport http.RoundTripper
}
func (t *TokenTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", "Bearer "+t.Token)
return t.Transport.RoundTrip(req)
}
func printHelp() {
helpText := `go2spec - A tool to package Go modules into RPM spec files.
@@ -29,13 +41,28 @@ If there are no commands provided, the tool will default to executing the 'pack'
}
func main() {
transport := github.BasicAuthTransport{
Username: os.Getenv("GITHUB_USERNAME"),
Password: os.Getenv("GITHUB_PASSWORD"),
OTP: os.Getenv("GITHUB_OTP"),
Transport: httpcache.NewMemoryCacheTransport(),
token := os.Getenv("GITHUB_TOKEN")
var client *http.Client
if token != "" {
// Use token authentication for better rate limits
client = &http.Client{
Transport: &TokenTransport{
Token: token,
Transport: httpcache.NewMemoryCacheTransport(),
},
}
} else {
// Fallback to basic auth if token is not provided
transport := github.BasicAuthTransport{
Username: os.Getenv("GITHUB_USERNAME"),
Password: os.Getenv("GITHUB_PASSWORD"),
OTP: os.Getenv("GITHUB_OTP"),
Transport: httpcache.NewMemoryCacheTransport(),
}
client = transport.Client()
}
gitHub = github.NewClient(transport.Client())
gitHub = github.NewClient(client)
args := os.Args[1:]