author | Mikael Berthe <mikael@lilotux.net> |
Fri, 28 Apr 2017 15:39:32 +0200 | |
changeset 20 | b0ccc09f07a2 |
parent 19 | 4c81f4393773 |
child 22 | 5778b09bc6fe |
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 { |
|
19
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
17 |
list, clear, dismiss bool |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
18 |
notifID int |
0 | 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 |
|
19
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
28 |
madonctl accounts notifications --dismiss --notification-id N |
0 | 29 |
madonctl accounts notifications --notification-id N`, |
30 |
//Long: `TBW...`, |
|
31 |
RunE: notificationRunE, |
|
32 |
} |
|
33 |
||
34 |
func init() { |
|
35 |
accountsCmd.AddCommand(notificationsCmd) |
|
36 |
||
37 |
notificationsCmd.Flags().BoolVar(¬ificationsOpts.list, "list", false, "List all current notifications") |
|
38 |
notificationsCmd.Flags().BoolVar(¬ificationsOpts.clear, "clear", false, "Clear all current notifications") |
|
19
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
39 |
notificationsCmd.Flags().BoolVar(¬ificationsOpts.dismiss, "dismiss", false, "Delete a notification") |
0 | 40 |
notificationsCmd.Flags().IntVar(¬ificationsOpts.notifID, "notification-id", 0, "Get a notification") |
41 |
} |
|
42 |
||
43 |
func notificationRunE(cmd *cobra.Command, args []string) error { |
|
44 |
opt := notificationsOpts |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
45 |
var limOpts *madon.LimitParams |
0 | 46 |
|
47 |
if !opt.list && !opt.clear && opt.notifID < 1 { |
|
48 |
return errors.New("missing parameters") |
|
49 |
} |
|
50 |
||
51 |
if err := madonInit(true); err != nil { |
|
52 |
return err |
|
53 |
} |
|
54 |
||
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
55 |
if accountsOpts.limit > 0 { |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
56 |
limOpts = new(madon.LimitParams) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
57 |
limOpts.Limit = int(accountsOpts.limit) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
58 |
} |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
59 |
|
0 | 60 |
var obj interface{} |
61 |
var err error |
|
62 |
||
63 |
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
|
64 |
var notifications []madon.Notification |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
65 |
notifications, err = gClient.GetNotifications(limOpts) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
66 |
if accountsOpts.limit > 0 && len(notifications) > int(accountsOpts.limit) { |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
67 |
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
|
68 |
} |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
69 |
obj = notifications |
0 | 70 |
} else if opt.notifID > 0 { |
19
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
71 |
if opt.dismiss { |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
72 |
err = gClient.DismissNotification(opt.notifID) |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
73 |
} else { |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
74 |
obj, err = gClient.GetNotification(opt.notifID) |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
75 |
} |
0 | 76 |
} |
77 |
||
78 |
if err == nil && opt.clear { |
|
79 |
err = gClient.ClearNotifications() |
|
80 |
} |
|
81 |
||
82 |
if err != nil { |
|
83 |
errPrint("Error: %s", err.Error()) |
|
84 |
return nil |
|
85 |
} |
|
86 |
if obj == nil { |
|
87 |
return nil |
|
88 |
} |
|
89 |
||
90 |
p, err := getPrinter() |
|
91 |
if err != nil { |
|
92 |
return err |
|
93 |
} |
|
94 |
return p.PrintObj(obj, nil, "") |
|
95 |
} |