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() {
// 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
}