From 4d1cae4002337492170b204a5614a9278179e0d1 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Tue, 15 Aug 2023 19:00:22 +0700 Subject: [PATCH] feat: make github client optional --- README.md | 4 ++++ cmd/update-go-mod/main.go | 37 +++++++++++++++------------------ internal/cli/action_overlook.go | 5 +++++ 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index b5795cf..4e8cebb 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,10 @@ update-go-mod --deps-url "https://example.txt" # Don't do anythin update-go-mod --dry-run + +# Take a look +# Require GitHub access token in ~/.netrc +update-go-mod overlook ``` ## Thanks diff --git a/cmd/update-go-mod/main.go b/cmd/update-go-mod/main.go index 8286d6f..bacf5eb 100644 --- a/cmd/update-go-mod/main.go +++ b/cmd/update-go-mod/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "log" "strings" "github.com/google/go-github/v53/github" @@ -20,29 +19,27 @@ const ( func main() { // Prepare GitHub + var ghClient *github.Client netrcData, err := netrc.ParseFile(netrcPath) - if err != nil { - log.Fatalln("Failed to read file netrc", err) - } + if err == nil { + var ghAccessToken string + for _, machine := range netrcData.Machines { + if machine.Name == netrcMachineGitHub { + ghAccessToken = machine.Password + break + } + } - var ghAccessToken string - for _, machine := range netrcData.Machines { - if machine.Name == netrcMachineGitHub { - ghAccessToken = machine.Password - break + if ghAccessToken != "" { + ghTokenSrc := oauth2.StaticTokenSource( + &oauth2.Token{ + AccessToken: strings.TrimSpace(ghAccessToken), + }, + ) + ghHTTPClient := oauth2.NewClient(context.Background(), ghTokenSrc) + ghClient = github.NewClient(ghHTTPClient) } } - if ghAccessToken == "" { - log.Fatalln("Empty GitHub token in netrc") - } - - ghTokenSrc := oauth2.StaticTokenSource( - &oauth2.Token{ - AccessToken: strings.TrimSpace(ghAccessToken), - }, - ) - ghHTTPClient := oauth2.NewClient(context.Background(), ghTokenSrc) - ghClient := github.NewClient(ghHTTPClient) app := cli.NewApp(ghClient) app.Run() diff --git a/internal/cli/action_overlook.go b/internal/cli/action_overlook.go index 5ae6fff..fc27881 100644 --- a/internal/cli/action_overlook.go +++ b/internal/cli/action_overlook.go @@ -20,6 +20,11 @@ type GitHubRepoData struct { } func (a *action) Overlook(c *cli.Context) error { + // Optional + if a.ghClient == nil { + return nil + } + a.getFlags(c) mapImportedModules, err := a.runGetImportedModules(c)