-
Notifications
You must be signed in to change notification settings - Fork 7
/
sparkcli.go
589 lines (580 loc) · 15.5 KB
/
sparkcli.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// Package main provides the main entry point for sparkcli.
package main
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/tdeckers/sparkcli/api"
"github.com/tdeckers/sparkcli/util"
"log" // TODO: change to https://github.com/Sirupsen/logrus
"os"
"strings"
)
//
func main() {
var jsonFlag bool
config := util.GetConfiguration()
config.Load()
client := util.NewClient(config)
app := cli.NewApp()
app.Name = "sparkcli"
app.Usage = "Command Line Interface for Cisco Spark"
app.Version = "0.6.0"
app.Flags = []cli.Flag{
cli.BoolTFlag{
Name: "j",
Usage: "return results as json",
Destination: &jsonFlag,
},
}
app.Commands = []cli.Command{
{
Name: "login",
Aliases: []string{"l"},
Usage: "login to Cisco Spark",
Action: func(c *cli.Context) {
log.Println("Logging in")
login := util.NewLogin(config, client)
login.Authorize()
},
},
{
Name: "rooms",
Aliases: []string{"r"},
Usage: "operations on rooms",
Subcommands: []cli.Command{
{
Name: "list",
Aliases: []string{"l"},
Usage: "list all rooms",
Action: func(c *cli.Context) {
roomService := api.RoomService{Client: client}
rooms, err := roomService.List()
if err != nil {
log.Fatalln(err)
} else {
if jsonFlag {
util.PrintJson(rooms)
} else {
// TODO: should I calculate room id length somehow?
fmt.Print("Id" + strings.Repeat(" ", 76) + "Title\n")
for _, room := range *rooms {
fmt.Printf("%s: %s\n", room.Id, room.Title)
}
}
}
},
},
{
Name: "create",
Aliases: []string{"c"},
Usage: "create a new room",
Action: func(c *cli.Context) {
if c.NArg() != 1 {
log.Fatal("Usage: sparkcli rooms create <name>")
}
name := c.Args().Get(0)
roomService := api.RoomService{Client: client}
room, err := roomService.Create(name)
if err != nil {
log.Fatalln(err)
} else {
if jsonFlag {
util.PrintJson(room)
} else {
// Print just roomId, so can assign to env variable if desired.
fmt.Print(room.Id)
}
}
},
},
{
Name: "get",
Aliases: []string{"g"},
Usage: "get room details",
Action: func(c *cli.Context) {
if c.NArg() > 1 {
log.Fatal("Usage: sparkcli rooms get <id>")
}
id := c.Args().Get(0)
if id == "" { // try default room
id = config.DefaultRoomId
if id == "" {
log.Fatal("Usage: sparkcli rooms get <id> (no default room configured)")
}
}
roomService := api.RoomService{Client: client}
room, err := roomService.Get(id)
if err != nil {
log.Fatalln(err)
} else {
if jsonFlag {
util.PrintJson(room)
} else {
fmt.Printf("Id: %s\n", room.Id)
fmt.Printf("Title: %s\n", room.Title)
fmt.Printf("Sip Address: %s\n", room.SipAddress)
fmt.Printf("Created: %s\n", room.Created)
}
}
},
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "delete a room",
Action: func(c *cli.Context) {
if c.NArg() != 1 {
log.Fatal("Usage: sparkcli rooms delete <id>")
}
id := c.Args().Get(0)
roomService := api.RoomService{Client: client}
err := roomService.Delete(id)
//TODO: if error is '400 Bad Request', try deleting by name?
if err != nil {
log.Fatalln(err)
} else {
if !jsonFlag {
fmt.Println("Room deleted.")
}
// when json, just return empty. Exit code will tell it's ok.
}
},
},
// Convenience actions (not available in Cisco Spark API)
{
Name: "default",
Usage: "save default room in config",
Action: func(c *cli.Context) {
if c.NArg() > 1 {
log.Fatal("Usage: sparkcli rooms default (<id>)")
}
if c.NArg() == 1 {
id := c.Args().Get(0)
config.DefaultRoomId = id
config.Save()
} else {
// just display the room id
fmt.Print(config.DefaultRoomId)
}
},
},
},
},
{
Name: "messages",
Aliases: []string{"m"},
Usage: "operations on messages",
Subcommands: []cli.Command{
{
Name: "list",
Aliases: []string{"l"},
Usage: "list all messages",
Action: func(c *cli.Context) {
// TODO: add limiters (num, before, beforeMessage)
// If no arg provided, also use default room.
if c.NArg() > 1 {
log.Fatal("Usage: sparkcli messages list <roomid>")
}
id := c.Args().Get(0)
if id == "" {
id = config.DefaultRoomId
if id == "" {
log.Println("No default room configured.")
log.Fatal("Usage: sparkcli messages list <roomId>")
}
}
msgService := api.MessageService{Client: client}
msgs, err := msgService.List(id)
if err != nil {
log.Fatalln(err)
} else {
if jsonFlag {
util.PrintJson(msgs)
} else {
for _, msg := range *msgs {
fmt.Printf("[%v] %v: %v\n", msg.Created, msg.PersonEmail, msg.Text)
}
}
}
},
},
{
Name: "create",
Aliases: []string{"c"},
Usage: "create a new message",
Subcommands: []cli.Command{
{
Name: "text",
Usage: "create a new text message",
Action: func(c *cli.Context) {
// TODO: change this to take all args after the second as additional text.
if c.NArg() < 1 {
log.Fatal("Usage: sparkcli messages create text <room> <msg>")
}
id := c.Args().Get(0)
if id == "-" {
id = config.DefaultRoomId
if id == "" {
log.Println("No default room configured.")
log.Fatal("Usage: sparkcli messages list <roomId>")
}
}
msgTxt := strings.Join(c.Args().Tail(), " ")
msgService := api.MessageService{Client: client}
msg, err := msgService.Create(id, msgTxt)
if err != nil {
log.Fatalln(err)
} else {
if jsonFlag {
util.PrintJson(msg)
} else {
fmt.Print(msg.Id)
}
}
},
},
{
Name: "file",
Usage: "send a attachment",
Action: func(c *cli.Context) {
if c.NArg() < 1 {
log.Fatal("Usage: sparkcli messages create file <room> <file>")
}
id := c.Args().Get(0)
if id == "-" {
id = config.DefaultRoomId
if id == "" {
log.Println("No default room configured.")
log.Fatal("Usage: sparkcli messages list <roomId>")
}
}
filePath := strings.Join(c.Args().Tail(), " ")
msgService := api.MessageService{Client: client}
msg, err := msgService.CreateFile(id, filePath)
if err != nil {
log.Fatalln(err)
} else {
if jsonFlag {
util.PrintJson(msg)
} else {
fmt.Print(msg.Id)
}
}
},
},
},
},
{
Name: "get",
Aliases: []string{"g"},
Usage: "get message details",
Action: func(c *cli.Context) {
if c.NArg() != 1 {
log.Fatal("Usage: sparkcli messages get <id>")
}
id := c.Args().Get(0)
msgService := api.MessageService{Client: client}
msg, err := msgService.Get(id)
if err != nil {
log.Fatalln(err)
} else {
if jsonFlag {
util.PrintJson(msg)
} else {
fmt.Printf("Id: %s\n", msg.Id)
fmt.Printf("PersonId: %s\n", msg.PersonId)
fmt.Printf("PersonEmail: %s\n", msg.PersonEmail)
fmt.Printf("RoomId: %s\n", msg.RoomId)
fmt.Printf("Text: %s\n", msg.Text)
fmt.Printf("ToPersonId: %s\n", msg.ToPersonId)
fmt.Printf("ToPersonEmail: %s\n", msg.ToPersonEmail)
fmt.Printf("Created: %s\n", msg.Created)
}
}
},
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "delete a message",
Action: func(c *cli.Context) {
if c.NArg() != 1 {
log.Fatal("Usage: sparkcli messages delete <id>")
}
id := c.Args().Get(0)
msgService := api.MessageService{Client: client}
err := msgService.Delete(id)
if err != nil {
log.Fatalln(err)
} else {
if !jsonFlag {
fmt.Print("Message deleted.")
} // for json, don't print. Exit code = 0.
}
},
},
},
},
{
Name: "people",
Aliases: []string{"p"},
Usage: "operations on people",
Subcommands: []cli.Command{
{
Name: "get",
Aliases: []string{"g"},
Usage: "get your details",
Action: func(c *cli.Context) {
id := "me"
if c.NArg() == 1 { // if argument, use that as id
id = c.Args().Get(0)
}
peopleService := api.PeopleService{Client: client}
person, err := peopleService.Get(id)
if err != nil {
log.Fatalln(err)
} else {
if jsonFlag {
util.PrintJson(person)
} else {
fmt.Printf("Id: %s\n", person.Id)
fmt.Printf("Name: %s\n", person.DisplayName)
for _, email := range person.Emails {
fmt.Printf("Email: %s\n", email)
}
fmt.Printf("Avatar: %s\n", person.Avatar)
fmt.Printf("Created: %s\n", person.Created)
}
}
},
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "list people",
Flags: []cli.Flag{
cli.StringFlag{
Name: "email, e",
Usage: "email to search for",
},
cli.StringFlag{
Name: "name, n",
Usage: "name to search for (startWith function)",
},
},
Action: func(c *cli.Context) {
email := c.String("email")
name := c.String("name")
peopleService := api.PeopleService{Client: client}
people, err := peopleService.List(email, name)
if err != nil {
log.Fatalln(err)
} else {
if jsonFlag {
util.PrintJson(people)
} else {
for _, person := range *people {
fmt.Printf("%s:\n", person.Id)
fmt.Printf(" Name: %s\n", person.DisplayName)
fmt.Printf(" Email: ")
for i, email := range person.Emails {
if i == 0 {
fmt.Print(email)
} else {
fmt.Printf(", %s", email)
}
}
fmt.Println()
fmt.Printf(" Avatar: %s\n", person.Avatar)
fmt.Printf(" Created: %s\n", person.Created)
}
}
}
},
},
},
},
{
Name: "memberships",
Aliases: []string{"ms"},
Usage: "operations on memberships",
Subcommands: []cli.Command{
{
Name: "list",
Aliases: []string{"l"},
Usage: "list memberships",
Flags: []cli.Flag{
cli.StringFlag{
Name: "room, r",
Usage: "search by room id",
},
cli.StringFlag{
Name: "personid, p",
Usage: "filter by person id",
},
cli.StringFlag{
Name: "email, e",
Usage: "filter by email",
},
},
Action: func(c *cli.Context) {
roomId := c.String("room")
if roomId == "-" {
roomId = config.DefaultRoomId
if roomId == "" {
log.Println("No default room configured.")
log.Fatal("Usage: sparkcli memberships list -r <roomId>")
}
}
personId := c.String("personid")
personEmail := c.String("email")
memberService := api.MemberService{Client: client}
mss, err := memberService.List(roomId, personId, personEmail)
if err != nil {
log.Fatalln(err)
} else {
if jsonFlag {
util.PrintJson(mss)
} else {
for _, ms := range *mss {
fmt.Printf("%s:\n", ms.Id)
fmt.Printf(" Name: %s\n", ms.PersonDisplayName)
fmt.Printf(" Email: %s\n", ms.PersonEmail)
fmt.Printf(" Room: %s\n", ms.RoomId)
fmt.Printf(" Created: %s\n", ms.Created)
}
}
}
},
},
{
Name: "create",
Aliases: []string{"c"},
Usage: "create memberships",
Flags: []cli.Flag{
cli.StringFlag{
Name: "room, r",
Usage: "room to add person to",
},
cli.StringFlag{
Name: "personid, p",
Usage: "id of person to add",
},
cli.StringFlag{
Name: "email, e",
Usage: "email of person to add",
},
},
Action: func(c *cli.Context) {
roomId := c.String("room")
if roomId == "-" {
roomId = config.DefaultRoomId
if roomId == "" {
log.Println("No default room configured.")
log.Fatal("Usage: sparkcli memberships create -r <roomId> ...")
}
}
personId := c.String("personid")
personEmail := c.String("email")
memberService := api.MemberService{Client: client}
ms, err := memberService.Create(roomId, personId, personEmail)
if err != nil {
log.Fatalln(err)
} else {
if jsonFlag {
util.PrintJson(ms)
} else {
fmt.Printf("Id: %s\n", ms.Id)
fmt.Printf("Name: %s\n", ms.PersonDisplayName)
fmt.Printf("Email: %s\n", ms.PersonEmail)
fmt.Printf("Room: %s\n", ms.RoomId)
fmt.Printf("Created: %s\n", ms.Created)
}
}
},
},
{
Name: "get",
Aliases: []string{"g"},
Usage: "get membership details",
Action: func(c *cli.Context) {
if c.NArg() != 1 {
log.Fatal("Usage: sparkcli memberships get <id>")
}
id := c.Args().Get(0)
msService := api.MemberService{Client: client}
ms, err := msService.Get(id)
if err != nil {
log.Fatalln(err)
} else {
if jsonFlag {
util.PrintJson(ms)
} else {
fmt.Printf("Id: %s\n", ms.Id)
fmt.Printf("Name: %s\n", ms.PersonDisplayName)
fmt.Printf("Email: %s\n", ms.PersonEmail)
fmt.Printf("Room: %s\n", ms.RoomId)
fmt.Printf("Created: %s\n", ms.Created)
}
}
},
},
{
Name: "update",
Aliases: []string{"u"},
Usage: "update membership",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "moderator, m",
Usage: "set moderator role for the membership",
},
},
Action: func(c *cli.Context) {
if c.NArg() != 1 {
log.Fatal("Usage: sparkcli memberships update -moderator <id>")
}
id := c.Args().Get(0)
// TODO: avoid doing update if flag is not present.
moderator := c.Bool("moderator")
msService := api.MemberService{Client: client}
ms, err := msService.Update(id, moderator)
if err != nil {
log.Fatalln(err)
} else {
if jsonFlag {
util.PrintJson(ms)
} else {
fmt.Printf("Id: %s\n", ms.Id)
fmt.Printf("Name: %s\n", ms.PersonDisplayName)
fmt.Printf("Email: %s\n", ms.PersonEmail)
fmt.Printf("Room: %s\n", ms.RoomId)
fmt.Printf("Created: %s\n", ms.Created)
}
}
},
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "delete membership",
Action: func(c *cli.Context) {
if c.NArg() != 1 {
log.Fatal("Usage: sparkcli memberships delete <id>")
}
id := c.Args().Get(0)
msService := api.MemberService{Client: client}
err := msService.Delete(id)
if err != nil {
log.Fatalln(err)
} else {
if !jsonFlag {
fmt.Println("Membership deleted.")
}
}
},
},
},
},
}
app.Run(os.Args)
}