Debian Codegraph @

server/cmd/client/client.go

raw
1 package main
2
3 import (
4 "encoding/json"
5 "fmt"
6 "io"
7 "log"
8 "net"
9 "os"
10
11 "github.com/henrybear327/go-proton-api/server/proto"
12 "github.com/urfave/cli/v2"
13 "google.golang.org/grpc"
14 "google.golang.org/grpc/credentials/insecure"
15 )
16
17 func main() {
18 app := cli.NewApp()
19
20 app.Flags = []cli.Flag{
21 &cli.StringFlag{
22 Name: "host",
23 Usage: "host to connect to",
24 Value: "localhost",
25 },
26 &cli.IntFlag{
27 Name: "port",
28 Usage: "port to connect to",
29 Value: 8080,
30 },
31 }
32
33 app.Commands = []*cli.Command{
34 {
35 Name: "info",
36 Action: getInfoAction,
37 },
38 {
39 Name: "auth",
40 Subcommands: []*cli.Command{
41 {
42 Name: "revoke",
43 Action: revokeUserAction,
44 Flags: []cli.Flag{
45 &cli.StringFlag{
46 Name: "userID",
47 Usage: "user ID to revoke",
48 Required: true,
49 },
50 },
51 },
52 },
53 },
54 {
55 Name: "user",
56 Subcommands: []*cli.Command{
57 {
58 Name: "create",
59 Action: createUserAction,
60 Flags: []cli.Flag{
61 &cli.StringFlag{
62 Name: "username",
63 Usage: "username of the account",
64 Required: true,
65 },
66 &cli.StringFlag{
67 Name: "password",
68 Usage: "password of the account",
69 Required: true,
70 },
71 },
72 },
73 },
74 },
75 {
76 Name: "address",
77 Subcommands: []*cli.Command{
78 {
79 Name: "create",
80 Action: createAddressAction,
81 Flags: []cli.Flag{
82 &cli.StringFlag{
83 Name: "userID",
84 Usage: "ID of the user to create the address for",
85 Required: true,
86 },
87 &cli.StringFlag{
88 Name: "email",
89 Usage: "email of the account",
90 Required: true,
91 },
92 &cli.StringFlag{
93 Name: "password",
94 Usage: "password of the account",
95 Required: true,
96 },
97 },
98 },
99 {
100 Name: "remove",
101 Action: removeAddressAction,
102 Flags: []cli.Flag{
103 &cli.StringFlag{
104 Name: "userID",
105 Usage: "ID of the user to remove the address from",
106 Required: true,
107 },
108 &cli.StringFlag{
109 Name: "addressID",
110 Usage: "ID of the address to remove",
111 Required: true,
112 },
113 },
114 },
115 },
116 },
117 {
118 Name: "label",
119 Subcommands: []*cli.Command{
120 {
121 Name: "create",
122 Action: createLabelAction,
123 Flags: []cli.Flag{
124 &cli.StringFlag{
125 Name: "userID",
126 Usage: "ID of the user to create the label for",
127 Required: true,
128 },
129 &cli.StringFlag{
130 Name: "name",
131 Usage: "name of the label",
132 Required: true,
133 },
134 &cli.StringFlag{
135 Name: "parentID",
136 Usage: "the ID of the parent label",
137 },
138 &cli.BoolFlag{
139 Name: "exclusive",
140 Usage: "Create an exclusive label (i.e. a folder)",
141 },
142 },
143 },
144 },
145 },
146 }
147
148 if err := app.Run(os.Args); err != nil {
149 log.Fatal(err)
150 }
151 }
152
153 func getInfoAction(c *cli.Context) error {
154 client, err := newServerClient(c)
155 if err != nil {
156 return err
157 }
158
159 res, err := client.GetInfo(c.Context, &proto.GetInfoRequest{})
160 if err != nil {
161 return err
162 }
163
164 return pretty(c.App.Writer, res)
165 }
166
167 func createUserAction(c *cli.Context) error {
168 client, err := newServerClient(c)
169 if err != nil {
170 return err
171 }
172
173 res, err := client.CreateUser(c.Context, &proto.CreateUserRequest{
174 Username: c.String("username"),
175 Password: []byte(c.String("password")),
176 })
177 if err != nil {
178 return err
179 }
180
181 return pretty(c.App.Writer, res)
182 }
183
184 func revokeUserAction(c *cli.Context) error {
185 client, err := newServerClient(c)
186 if err != nil {
187 return err
188 }
189
190 res, err := client.RevokeUser(c.Context, &proto.RevokeUserRequest{
191 UserID: c.String("userID"),
192 })
193 if err != nil {
194 return err
195 }
196
197 return pretty(c.App.Writer, res)
198 }
199
200 func createAddressAction(c *cli.Context) error {
201 client, err := newServerClient(c)
202 if err != nil {
203 return err
204 }
205
206 res, err := client.CreateAddress(c.Context, &proto.CreateAddressRequest{
207 UserID: c.String("userID"),
208 Email: c.String("email"),
209 Password: []byte(c.String("password")),
210 })
211 if err != nil {
212 return err
213 }
214
215 return pretty(c.App.Writer, res)
216 }
217
218 func removeAddressAction(c *cli.Context) error {
219 client, err := newServerClient(c)
220 if err != nil {
221 return err
222 }
223
224 res, err := client.RemoveAddress(c.Context, &proto.RemoveAddressRequest{
225 UserID: c.String("userID"),
226 AddrID: c.String("addressID"),
227 })
228 if err != nil {
229 return err
230 }
231
232 return pretty(c.App.Writer, res)
233 }
234
235 func createLabelAction(c *cli.Context) error {
236 client, err := newServerClient(c)
237 if err != nil {
238 return err
239 }
240
241 var labelType proto.LabelType
242
243 if c.Bool("exclusive") {
244 labelType = proto.LabelType_FOLDER
245 } else {
246 labelType = proto.LabelType_LABEL
247 }
248
249 res, err := client.CreateLabel(c.Context, &proto.CreateLabelRequest{
250 UserID: c.String("userID"),
251 Name: c.String("name"),
252 Type: labelType,
253 })
254 if err != nil {
255 return err
256 }
257
258 return pretty(c.App.Writer, res)
259 }
260
261 func newServerClient(c *cli.Context) (proto.ServerClient, error) {
262 cc, err := grpc.DialContext(
263 c.Context,
264 net.JoinHostPort(c.String("host"), fmt.Sprint(c.Int("port"))),
265 grpc.WithTransportCredentials(insecure.NewCredentials()),
266 )
267 if err != nil {
268 return nil, fmt.Errorf("failed to dial: %w", err)
269 }
270
271 return proto.NewServerClient(cc), nil
272 }
273
274 func pretty[T any](w io.Writer, v T) error {
275 enc, err := json.MarshalIndent(v, "", " ")
276 if err != nil {
277 return err
278 }
279
280 if _, err := w.Write(enc); err != nil {
281 return err
282 }
283
284 return nil
285 }