From 4724952c61a91f393c81c3f9db287b02f48e43f6 Mon Sep 17 00:00:00 2001 From: Shteryana Shopova Date: Sat, 20 Jul 2019 22:58:53 +0300 Subject: [PATCH] Rework the code a bit (no functional change) --- access/fetch-keys/main.go | 129 ++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 62 deletions(-) diff --git a/access/fetch-keys/main.go b/access/fetch-keys/main.go index 1ae4422..dd9b62e 100644 --- a/access/fetch-keys/main.go +++ b/access/fetch-keys/main.go @@ -55,69 +55,8 @@ func main() { client := github.NewClient(tc) - var targetTeam *github.Team - var teamMembers []*string - - for nextPage := 0; ; { - // list all teams for the specified org - opt := &github.ListOptions{nextPage, 50} - teams, rsp, err := client.Teams.ListTeams(ctx, *ghOrganization, opt) - - if err != nil { - fmt.Println("client.ListTeams error: ", err) - os.Exit(-1) - } - - if rsp == nil { - fmt.Println("client.ListTeams returned empty response: ", err) - } - - for _, team := range teams { - if *team.Name == *ghTeam { - targetTeam = team - break - } - } - - if rsp.NextPage == 0 || nextPage == rsp.NextPage { - break - } - nextPage = rsp.NextPage - } - - if targetTeam == nil { - fmt.Println(*ghTeam , " team not found in ", *ghOrganization) - os.Exit(2) - } - - for nextPage := 0; ; { - // list all members for the given organization's team - opt := &github.TeamListTeamMembersOptions{ - Role: "all", - ListOptions: github.ListOptions{nextPage, 50}, - } - - users, rsp, err := client.Teams.ListTeamMembers(context.Background(), *targetTeam.ID, opt) - - if err != nil { - fmt.Println("client.Teams.ListTeamMembers ", err) - os.Exit(-1) - } - - if rsp == nil { - fmt.Println("client.Teams.ListTeamMembers: ", err) - } - - for _, user := range users { - teamMembers = append(teamMembers, user.Login) - } - - if rsp.NextPage == 0 || nextPage == rsp.NextPage { - break - } - nextPage = rsp.NextPage - } + teamMembers := fetchUsers(client, ghOrganization, ghTeam) for _, user := range teamMembers { if *quiet == false { fmt.Println("Fetching keys for ", *user) @@ -164,3 +103,69 @@ func main() { os.Exit(0) } + +func fetchUsers(client *github.Client, org *string, team *string) (teamMembers []*string) { + var targetTeam *github.Team + + for nextPage := 0; ; { + // list all teams for the specified org + opt := &github.ListOptions{nextPage, 50} + teams, rsp, err := client.Teams.ListTeams(context.Background(), *org, opt) + + if err != nil { + fmt.Println("client.ListTeams error: ", err) + os.Exit(-1) + } + + if rsp == nil { + fmt.Println("client.ListTeams returned empty response: ", err) + } + + for _, ghTeam := range teams { + if *ghTeam.Name == *team { + targetTeam = ghTeam + break + } + } + + if rsp.NextPage == 0 || nextPage == rsp.NextPage { + break + } + nextPage = rsp.NextPage + } + + if targetTeam == nil { + fmt.Println(*team , " team not found in ", *org) + os.Exit(2) + } + + for nextPage := 0; ; { + // list all members for the given organization's team + opt := &github.TeamListTeamMembersOptions{ + Role: "all", + ListOptions: github.ListOptions{nextPage, 50}, + } + + users, rsp, err := client.Teams.ListTeamMembers(context.Background(), *targetTeam.ID, opt) + + if err != nil { + fmt.Println("client.Teams.ListTeamMembers ", err) + os.Exit(-1) + } + + if rsp == nil { + fmt.Println("client.Teams.ListTeamMembers: ", err) + } + + for _, user := range users { + teamMembers = append(teamMembers, user.Login) + } + + if rsp.NextPage == 0 || nextPage == rsp.NextPage { + break + } + nextPage = rsp.NextPage + } + + return teamMembers +}