-
Notifications
You must be signed in to change notification settings - Fork 114
/
main.go
68 lines (57 loc) · 1.44 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"github.com/Khan/genqlient/graphql"
)
type authedTransport struct {
key string
wrapped http.RoundTripper
}
func (t *authedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", "bearer "+t.key)
return t.wrapped.RoundTrip(req)
}
func main() {
var err error
defer func() {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}()
key := os.Getenv("GITHUB_TOKEN")
if key == "" {
err = fmt.Errorf("must set GITHUB_TOKEN=<github token>")
return
}
httpClient := http.Client{
Transport: &authedTransport{
key: key,
wrapped: http.DefaultTransport,
},
}
graphqlClient := graphql.NewClient("https://api.github.com/graphql", &httpClient)
switch len(os.Args) {
case 1:
var viewerResp *getViewerResponse
viewerResp, err = getViewer(context.Background(), graphqlClient)
if err != nil {
return
}
fmt.Println("you are", viewerResp.Viewer.MyName, "created on", viewerResp.Viewer.CreatedAt.Format("2006-01-02"))
case 2:
username := os.Args[1]
var userResp *getUserResponse
userResp, err = getUser(context.Background(), graphqlClient, username)
if err != nil {
return
}
fmt.Println(username, "is", userResp.User.TheirName, "created on", userResp.User.CreatedAt.Format("2006-01-02"))
default:
err = fmt.Errorf("usage: %v [username]", os.Args[0])
}
}
//go:generate go run github.com/Khan/genqlient genqlient.yaml