feat: make github client optional

main
sudo pacman -Syu 2023-08-15 19:00:22 +07:00 committed by sudo pacman -Syu
parent 22d16a8947
commit 4d1cae4002
3 changed files with 26 additions and 20 deletions

View File

@ -23,6 +23,10 @@ update-go-mod --deps-url "https://example.txt"
# Don't do anythin # Don't do anythin
update-go-mod --dry-run update-go-mod --dry-run
# Take a look
# Require GitHub access token in ~/.netrc
update-go-mod overlook
``` ```
## Thanks ## Thanks

View File

@ -2,7 +2,6 @@ package main
import ( import (
"context" "context"
"log"
"strings" "strings"
"github.com/google/go-github/v53/github" "github.com/google/go-github/v53/github"
@ -20,11 +19,9 @@ const (
func main() { func main() {
// Prepare GitHub // Prepare GitHub
var ghClient *github.Client
netrcData, err := netrc.ParseFile(netrcPath) netrcData, err := netrc.ParseFile(netrcPath)
if err != nil { if err == nil {
log.Fatalln("Failed to read file netrc", err)
}
var ghAccessToken string var ghAccessToken string
for _, machine := range netrcData.Machines { for _, machine := range netrcData.Machines {
if machine.Name == netrcMachineGitHub { if machine.Name == netrcMachineGitHub {
@ -32,17 +29,17 @@ func main() {
break break
} }
} }
if ghAccessToken == "" {
log.Fatalln("Empty GitHub token in netrc")
}
if ghAccessToken != "" {
ghTokenSrc := oauth2.StaticTokenSource( ghTokenSrc := oauth2.StaticTokenSource(
&oauth2.Token{ &oauth2.Token{
AccessToken: strings.TrimSpace(ghAccessToken), AccessToken: strings.TrimSpace(ghAccessToken),
}, },
) )
ghHTTPClient := oauth2.NewClient(context.Background(), ghTokenSrc) ghHTTPClient := oauth2.NewClient(context.Background(), ghTokenSrc)
ghClient := github.NewClient(ghHTTPClient) ghClient = github.NewClient(ghHTTPClient)
}
}
app := cli.NewApp(ghClient) app := cli.NewApp(ghClient)
app.Run() app.Run()

View File

@ -20,6 +20,11 @@ type GitHubRepoData struct {
} }
func (a *action) Overlook(c *cli.Context) error { func (a *action) Overlook(c *cli.Context) error {
// Optional
if a.ghClient == nil {
return nil
}
a.getFlags(c) a.getFlags(c)
mapImportedModules, err := a.runGetImportedModules(c) mapImportedModules, err := a.runGetImportedModules(c)