author | Mikael Berthe <mikael@lilotux.net> |
Sun, 30 Apr 2017 20:45:40 +0200 | |
changeset 44 | 6da40ca4534c |
parent 30 | 14561d44211b |
child 45 | b58a7ea1aeb2 |
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 |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
18 |
notifID int64 |
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") |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
40 |
notificationsCmd.Flags().Int64Var(¬ificationsOpts.notifID, "notification-id", 0, "Get a notification") |
0 | 41 |
} |
42 |
||
43 |
func notificationRunE(cmd *cobra.Command, args []string) error { |
|
44 |
opt := notificationsOpts |
|
45 |
||
46 |
if !opt.list && !opt.clear && opt.notifID < 1 { |
|
47 |
return errors.New("missing parameters") |
|
48 |
} |
|
49 |
||
50 |
if err := madonInit(true); err != nil { |
|
51 |
return err |
|
52 |
} |
|
53 |
||
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
54 |
var limOpts *madon.LimitParams |
30
14561d44211b
Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
55 |
if accountsOpts.all || accountsOpts.limit > 0 || accountsOpts.sinceID > 0 || accountsOpts.maxID > 0 { |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
56 |
limOpts = new(madon.LimitParams) |
30
14561d44211b
Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
57 |
limOpts.All = accountsOpts.all |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
58 |
} |
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
59 |
|
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
60 |
if accountsOpts.limit > 0 { |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
61 |
limOpts.Limit = int(accountsOpts.limit) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
62 |
} |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
63 |
if accountsOpts.maxID > 0 { |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
64 |
limOpts.MaxID = int64(accountsOpts.maxID) |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
65 |
} |
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
66 |
if accountsOpts.sinceID > 0 { |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
67 |
limOpts.SinceID = int64(accountsOpts.sinceID) |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
68 |
} |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
69 |
|
0 | 70 |
var obj interface{} |
71 |
var err error |
|
72 |
||
73 |
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
|
74 |
var notifications []madon.Notification |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
75 |
notifications, err = gClient.GetNotifications(limOpts) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
76 |
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
|
77 |
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
|
78 |
} |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
79 |
obj = notifications |
0 | 80 |
} else if opt.notifID > 0 { |
19
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
81 |
if opt.dismiss { |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
82 |
err = gClient.DismissNotification(opt.notifID) |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
83 |
} else { |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
84 |
obj, err = gClient.GetNotification(opt.notifID) |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
85 |
} |
0 | 86 |
} |
87 |
||
88 |
if err == nil && opt.clear { |
|
89 |
err = gClient.ClearNotifications() |
|
90 |
} |
|
91 |
||
92 |
if err != nil { |
|
93 |
errPrint("Error: %s", err.Error()) |
|
94 |
return nil |
|
95 |
} |
|
96 |
if obj == nil { |
|
97 |
return nil |
|
98 |
} |
|
99 |
||
100 |
p, err := getPrinter() |
|
101 |
if err != nil { |
|
102 |
return err |
|
103 |
} |
|
104 |
return p.PrintObj(obj, nil, "") |
|
105 |
} |