refactor: move init github client

main
sudo pacman -Syu 2023-08-15 19:03:37 +07:00 committed by sudo pacman -Syu
parent 4d1cae4002
commit e1b77493c6
1 changed files with 29 additions and 21 deletions

View File

@ -18,29 +18,37 @@ const (
) )
func main() { func main() {
// Prepare GitHub app := cli.NewApp(
var ghClient *github.Client initGitHubClient(),
netrcData, err := netrc.ParseFile(netrcPath) )
if err == nil { app.Run()
var ghAccessToken string }
for _, machine := range netrcData.Machines {
if machine.Name == netrcMachineGitHub {
ghAccessToken = machine.Password
break
}
}
if ghAccessToken != "" { func initGitHubClient() *github.Client {
ghTokenSrc := oauth2.StaticTokenSource( netrcData, err := netrc.ParseFile(netrcPath)
&oauth2.Token{ if err != nil {
AccessToken: strings.TrimSpace(ghAccessToken), return nil
}, }
)
ghHTTPClient := oauth2.NewClient(context.Background(), ghTokenSrc) var ghAccessToken string
ghClient = github.NewClient(ghHTTPClient) for _, machine := range netrcData.Machines {
if machine.Name == netrcMachineGitHub {
ghAccessToken = machine.Password
break
} }
} }
app := cli.NewApp(ghClient) if ghAccessToken == "" {
app.Run() return nil
}
ghTokenSrc := oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: strings.TrimSpace(ghAccessToken),
},
)
ghHTTPClient := oauth2.NewClient(context.Background(), ghTokenSrc)
ghClient := github.NewClient(ghHTTPClient)
return ghClient
} }