func GetCredential() (Credential, error) { if _, err := os.Stat(credentialPath); os.IsNotExist(err) { utils.Dieif(err) } return parseCredential() }
func parseCredential() (Credential, error) { var credential Credential file, err := ioutil.ReadFile(credentialPath) utils.Dieif(err) err = json.Unmarshal(file, &credential) return credential, err }
func GetGithubClient() (*github.Client, string) { credential, err := GetCredential() utils.Dieif(err) ts := oauth2.StaticTokenSource( &oauth2.Token{ AccessToken: credential.OauthAccessToken, }, ) tc := oauth2.NewClient(oauth2.NoContext, ts) // TODO: error handling return github.NewClient(tc), credential.User }
// Dump github repository func DumpRepository() { var allRepos []github.Repository ghClient, user := credential.GetGithubClient() opt := &github.RepositoryListOptions{ ListOptions: github.ListOptions{PerPage: 100}, } for { repos, response, err := ghClient.Repositories.List(user, opt) utils.Dieif(err) allRepos = append(allRepos, repos...) if response.NextPage == 0 { break } else { opt.ListOptions.Page = response.NextPage } } showResultRepository(allRepos) //fmt.Println(*repo.HTMLURL) //wg.Wait() //for _, repo := range allRepos { // fmt.Println(*repo.HTMLURL) // TODO: goroutine //var wg sync.WaitGroup //count := 0 //for { // wg.Add(count) // count += 1 // go func() { // repos, response, err := ghClient.Repositories.List(user, opt) // utils.Dieif(err) // allRepos = append(allRepos, repos...) // if response.NextPage == 0 { // return // } else { // opt.ListOptions.Page = response.NextPage // } // wg.Done() // }() //} //wg.Wait() //for _, repo := range allRepos { // fmt.Println(*repo.HTMLURL) //} }
// Dump github starred repository func DumpStarredRepository() { var allRepos []github.StarredRepository ghClient, user := credential.GetGithubClient() opt := &github.ActivityListStarredOptions{ ListOptions: github.ListOptions{PerPage: 100}, } // TODO: goroutine for { repos, response, err := ghClient.Activity.ListStarred(user, opt) utils.Dieif(err) allRepos = append(allRepos, repos...) if response.NextPage == 0 { break } else { opt.ListOptions.Page = response.NextPage } } showResultStarredRepository(allRepos) }