author | Mikael Berthe <mikael@lilotux.net> |
Sun, 23 Apr 2017 23:32:16 +0200 | |
changeset 13 | f862af8faf17 |
parent 0 | 5abace724584 |
child 19 | 4c81f4393773 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net> |
2 |
// |
|
3 |
// Licensed under the MIT license. |
|
4 |
// Please see the LICENSE file is this directory. |
|
5 |
||
6 |
package cmd |
|
7 |
||
8 |
import ( |
|
9 |
"errors" |
|
10 |
||
11 |
"github.com/spf13/cobra" |
|
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
12 |
|
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
13 |
"github.com/McKael/madon" |
0 | 14 |
) |
15 |
||
16 |
var notificationsOpts struct { |
|
17 |
list, clear bool |
|
18 |
notifID int |
|
19 |
} |
|
20 |
||
21 |
// notificationsCmd represents the notifications subcommand |
|
22 |
var notificationsCmd = &cobra.Command{ |
|
23 |
Use: "notifications", // XXX |
|
24 |
Aliases: []string{"notification", "notif"}, |
|
25 |
Short: "Manage notifications", |
|
26 |
Example: ` madonctl accounts notifications --list |
|
27 |
madonctl accounts notifications --list --clear |
|
28 |
madonctl accounts notifications --notification-id N`, |
|
29 |
//Long: `TBW...`, |
|
30 |
RunE: notificationRunE, |
|
31 |
} |
|
32 |
||
33 |
func init() { |
|
34 |
accountsCmd.AddCommand(notificationsCmd) |
|
35 |
||
36 |
notificationsCmd.Flags().BoolVar(¬ificationsOpts.list, "list", false, "List all current notifications") |
|
37 |
notificationsCmd.Flags().BoolVar(¬ificationsOpts.clear, "clear", false, "Clear all current notifications") |
|
38 |
notificationsCmd.Flags().IntVar(¬ificationsOpts.notifID, "notification-id", 0, "Get a notification") |
|
39 |
} |
|
40 |
||
41 |
func notificationRunE(cmd *cobra.Command, args []string) error { |
|
42 |
opt := notificationsOpts |
|
43 |
||
44 |
if !opt.list && !opt.clear && opt.notifID < 1 { |
|
45 |
return errors.New("missing parameters") |
|
46 |
} |
|
47 |
||
48 |
if err := madonInit(true); err != nil { |
|
49 |
return err |
|
50 |
} |
|
51 |
||
52 |
var obj interface{} |
|
53 |
var err error |
|
54 |
||
55 |
if opt.list { |
|
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
56 |
var notifications []madon.Notification |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
57 |
notifications, err = gClient.GetNotifications() |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
58 |
if accountsOpts.limit > 0 { |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
59 |
notifications = notifications[:accountsOpts.limit] |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
60 |
} |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
61 |
obj = notifications |
0 | 62 |
} else if opt.notifID > 0 { |
63 |
obj, err = gClient.GetNotification(opt.notifID) |
|
64 |
} |
|
65 |
||
66 |
if err == nil && opt.clear { |
|
67 |
err = gClient.ClearNotifications() |
|
68 |
} |
|
69 |
||
70 |
if err != nil { |
|
71 |
errPrint("Error: %s", err.Error()) |
|
72 |
return nil |
|
73 |
} |
|
74 |
if obj == nil { |
|
75 |
return nil |
|
76 |
} |
|
77 |
||
78 |
p, err := getPrinter() |
|
79 |
if err != nil { |
|
80 |
return err |
|
81 |
} |
|
82 |
return p.PrintObj(obj, nil, "") |
|
83 |
} |