forked from nytimes/gizmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (97 loc) · 2.83 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
package main
import (
"context"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"os"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/oauth"
"google.golang.org/grpc/metadata"
readinglist "github.com/NYTimes/gizmo/examples/servers/reading-list"
)
var (
host = flag.String("host", "localhost:8081", "the host of the reading list server")
insecure = flag.Bool("insecure", false, "use an insecure connection")
mode = flag.String("mode", "list", "(list|update)")
// list
limit = flag.Int("limit", 20, "limit for the number of links to return when listing links")
// update
article = flag.String("url", "", "the URL to add or delete")
delete = flag.Bool("delete", false, "delete this URL from the list (requires -mode update)")
creds = flag.String("creds", "", "the path of the service account credentials file. if empty, uses Google Application Default Credentials.")
fakeID = flag.String("fakeID", "", "for local development - a user ID to inject into the request")
)
func main() {
flag.Parse()
ctx := context.Background()
var copts []grpc.CallOption
if *creds != "" {
cs, err := oauth.NewJWTAccessFromFile(*creds)
if err != nil {
exitf("%v", err)
}
copts = append(copts, grpc.PerRPCCredentials(cs))
}
// add a fake user to the metadata for local dev
if *fakeID != "" {
b, err := json.Marshal(map[string]string{"id": *fakeID})
if err != nil {
exitf("%v", err)
}
ctx = metadata.NewOutgoingContext(ctx, metadata.New(
map[string]string{
"x-endpoint-api-userinfo": base64.StdEncoding.EncodeToString(b),
}))
}
var dopts []grpc.DialOption
if *insecure {
dopts = append(dopts, grpc.WithInsecure())
} else {
cs, err := oauth.NewApplicationDefault(ctx)
if err != nil {
exitf("%v", err)
}
copts = append(copts, grpc.PerRPCCredentials(cs))
}
conn, err := grpc.Dial(*host, dopts...)
if err != nil {
exitf("%v", err)
}
c := readinglist.NewReadingListServiceClient(conn)
switch *mode {
case "list":
l, err := c.GetListLimit(ctx, &readinglist.GetListLimitRequest{Limit: int32(*limit)},
copts...)
if err != nil {
exitf("unable to get links: %s", err.Error())
}
fmt.Printf("successful request with %d links returned\n", len(l.Links))
for _, lk := range l.Links {
fmt.Println("* " + lk.Url)
}
case "update":
aurl := *article
if len(aurl) == 0 {
exitf("missing -url flag")
}
fmt.Println("saving URL:", aurl)
m, err := c.PutLink(ctx, &readinglist.PutLinkRequest{
Request: &readinglist.LinkRequest{
Link: &readinglist.Link{Url: aurl},
Delete: *delete}},
copts...)
if err != nil {
exitf("unable to update link: %v", err)
}
fmt.Println(m.Message)
default:
fmt.Println("INVALID MODE. Please choose 'update' or 'list'")
}
}
func exitf(format string, v ...interface{}) {
fmt.Fprintf(os.Stderr, format, v...)
fmt.Fprintln(os.Stderr)
os.Exit(2)
}