-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
85 lines (72 loc) · 2.16 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
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
"github.com/frankywahl/allowedSignatures/internal/github"
"github.com/frankywahl/allowedSignatures/internal/ssh"
)
var ghToken string
var githubEnteprise = "http://api.github.com/graphql"
var verbose bool
var owner, repo string
var useContributors bool
func main() {
ctx := context.Background()
if err := run(ctx); err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context) error {
if err := parseFlags(ctx); err != nil {
return err
}
opts := []github.Option{}
if verbose {
opts = append(opts, github.SetVerbose())
}
ghClient, err := github.NewEnterpriseClient(githubEnteprise, ghToken, opts...)
if err != nil {
return err
}
var users []github.User
if useContributors {
users, err = ghClient.GetContributorKeys(ctx, owner, repo)
} else {
users, err = ghClient.GetCollaboratorKeys(ctx, owner, repo)
}
if err != nil {
return err
}
if err := printOutput(os.Stdout, users); err != nil {
return err
}
return nil
}
func printOutput(w io.Writer, users []github.User) error {
for _, user := range users {
for _, key := range ssh.FilterSigningKeys(user.Keys) {
fmt.Fprintf(os.Stdout, "%s %s %s\n", user.Login, key, user.Login)
}
}
return nil
}
func parseFlags(ctx context.Context) error {
flag.StringVar(&ghToken, "github-token", os.Getenv("GITHUB_API_TOKEN"), "the github token to use to make requests\ndefaults to environment variable GITHUB_API_TOKEN")
flag.BoolVar(&verbose, "verbose", false, "print debugging information")
flag.BoolVar(&useContributors, "use-contributors", false, "use contributors to generate list. This is more complete, but will make many more requests to GitHub")
flag.StringVar(&repo, "repository", "", "the repository to get the information for")
flag.StringVar(&githubEnteprise, "github-enterprise", "https://api.github.com/graphql", "use github enterprise URL as the endpoint instead of standard GitHub")
flag.StringVar(&owner, "owner", "", "the organisation or owner of the repository")
flag.Parse()
if owner == "" {
return fmt.Errorf("owner cannot be blank")
}
if repo == "" {
return fmt.Errorf("repository cannot be blank")
}
return nil
}