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,10 +18,18 @@ const (
)
func main() {
// Prepare GitHub
var ghClient *github.Client
app := cli.NewApp(
initGitHubClient(),
)
app.Run()
}
func initGitHubClient() *github.Client {
netrcData, err := netrc.ParseFile(netrcPath)
if err == nil {
if err != nil {
return nil
}
var ghAccessToken string
for _, machine := range netrcData.Machines {
if machine.Name == netrcMachineGitHub {
@ -30,17 +38,17 @@ func main() {
}
}
if ghAccessToken != "" {
if ghAccessToken == "" {
return nil
}
ghTokenSrc := oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: strings.TrimSpace(ghAccessToken),
},
)
ghHTTPClient := oauth2.NewClient(context.Background(), ghTokenSrc)
ghClient = github.NewClient(ghHTTPClient)
}
}
ghClient := github.NewClient(ghHTTPClient)
app := cli.NewApp(ghClient)
app.Run()
return ghClient
}