forked from Jimdo/github-team-ssh-keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
153 lines (135 loc) · 3.61 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"golang.org/x/oauth2"
"github.com/Luzifer/rconfig"
"github.com/google/go-github/github"
)
type user struct {
Login string
Key string
}
func listTeams(client *github.Client, org string) ([]*github.Team, error) {
page := 1
teams := []*github.Team{}
for {
teamsFromPage, resp, err := client.Organizations.ListTeams(
org,
&github.ListOptions{Page: page},
)
if err != nil {
return nil, fmt.Errorf("Error: on page %d fetching teams failed with %s", page, err)
}
teams = append(teams, teamsFromPage...)
if resp.NextPage == 0 || resp.LastPage == 0 {
// We're done here.
break
}
page = resp.NextPage
}
return teams, nil
}
func getTeam(client *github.Client, org string, name string) (*github.Team, error) {
teams, err := listTeams(client, org)
if err != nil {
return nil, fmt.Errorf("fetching teams failed: %s", err)
}
for _, team := range teams {
if *team.Name == name {
return team, nil
}
}
return nil, errors.New("Team not found")
}
func getMembers(client *github.Client, team *github.Team) ([]*github.User, error) {
page := 1
users := []*github.User{}
for {
usersFromPage, resp, err := client.Organizations.ListTeamMembers(
*team.ID,
&github.OrganizationListTeamMembersOptions{ListOptions: github.ListOptions{Page: page}},
)
if err != nil {
return nil, fmt.Errorf("Error: on page %d fetching team members failed with %s", page, err)
}
users = append(users, usersFromPage...)
if resp.NextPage == 0 || resp.LastPage == 0 {
// We're done here.
break
}
page = resp.NextPage
}
return users, nil
}
func getClient(token string) *github.Client {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
return client
}
func writeKeys(client *github.Client, w io.Writer, users []*github.User) error {
results := make([]user, 0)
for _, u := range users {
keys, _, err := client.Users.ListKeys(*u.Login, nil)
if err != nil {
return fmt.Errorf("Error when fetching keys: %s", err)
}
for _, key := range keys {
if key.Key == nil {
return fmt.Errorf("Key is null pointer for %s", *u.Login)
}
results = append(results, user{*u.Login, *key.Key})
break
}
}
b, _ := json.Marshal(results)
fmt.Fprintln(w, string(b))
return nil
}
func main() {
cfg := struct {
TeamName string `flag:"team" description:"Team to look for"`
GithubToken string `flag:"token" env:"GITHUB_TOKEN" description:"Github token"`
Organization string `flag:"org" description:"Github Organization"`
OutFile string `flag:"out,o" default:"-" description:"Output file"`
}{}
if err := rconfig.Parse(&cfg); err != nil {
log.Fatalf("Error parsing cli flags: %s", err)
}
client := getClient(cfg.GithubToken)
team, err := getTeam(client, cfg.Organization, cfg.TeamName)
if err != nil {
log.Fatalf("Getting team failed: %s", err)
}
members, err := getMembers(client, team)
if err != nil {
log.Fatalf("fetching members failed: %s", err)
}
switch cfg.OutFile {
case "-":
if err := writeKeys(client, os.Stdout, members); err != nil {
log.Fatalf("Unable to write keys: %s", err)
}
default:
o, err := os.Create(cfg.OutFile + ".tmp")
if err != nil {
log.Fatalf("Unable to open output file: %s", err)
}
if err := writeKeys(client, o, members); err != nil {
log.Fatalf("Unable to write keys: %s", err)
}
o.Close()
if err := os.Rename(cfg.OutFile+".tmp", cfg.OutFile); err != nil {
log.Fatalf("Moving tmp file to %s failed: %s", cfg.OutFile, err)
}
}
}