From e1b77493c62f10a82b4ca5f95d469a0a5e9290b9 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Tue, 15 Aug 2023 19:03:37 +0700 Subject: [PATCH] refactor: move init github client --- cmd/update-go-mod/main.go | 50 +++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/cmd/update-go-mod/main.go b/cmd/update-go-mod/main.go index bacf5eb..a836043 100644 --- a/cmd/update-go-mod/main.go +++ b/cmd/update-go-mod/main.go @@ -18,29 +18,37 @@ const ( ) func main() { - // Prepare GitHub - var ghClient *github.Client - netrcData, err := netrc.ParseFile(netrcPath) - if err == nil { - var ghAccessToken string - for _, machine := range netrcData.Machines { - if machine.Name == netrcMachineGitHub { - ghAccessToken = machine.Password - break - } - } + app := cli.NewApp( + initGitHubClient(), + ) + app.Run() +} - if ghAccessToken != "" { - ghTokenSrc := oauth2.StaticTokenSource( - &oauth2.Token{ - AccessToken: strings.TrimSpace(ghAccessToken), - }, - ) - ghHTTPClient := oauth2.NewClient(context.Background(), ghTokenSrc) - ghClient = github.NewClient(ghHTTPClient) +func initGitHubClient() *github.Client { + netrcData, err := netrc.ParseFile(netrcPath) + if err != nil { + return nil + } + + var ghAccessToken string + for _, machine := range netrcData.Machines { + if machine.Name == netrcMachineGitHub { + ghAccessToken = machine.Password + break } } - app := cli.NewApp(ghClient) - app.Run() + if ghAccessToken == "" { + return nil + } + + ghTokenSrc := oauth2.StaticTokenSource( + &oauth2.Token{ + AccessToken: strings.TrimSpace(ghAccessToken), + }, + ) + ghHTTPClient := oauth2.NewClient(context.Background(), ghTokenSrc) + ghClient := github.NewClient(ghHTTPClient) + + return ghClient }