author | Mikael Berthe <mikael@lilotux.net> |
Sat, 13 May 2017 11:56:52 +0200 | |
changeset 131 | 6461e0637767 |
parent 110 | 57843255fd1a |
child 165 | e76422bfe6ee |
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 ( |
|
47
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
9 |
"os" |
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
10 |
|
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
11 |
"github.com/pkg/errors" |
0 | 12 |
"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
|
13 |
|
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
14 |
"github.com/McKael/madon" |
0 | 15 |
) |
16 |
||
17 |
var notificationsOpts struct { |
|
19
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
18 |
list, clear, dismiss bool |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
19 |
notifID int64 |
0 | 20 |
} |
21 |
||
22 |
// notificationsCmd represents the notifications subcommand |
|
23 |
var notificationsCmd = &cobra.Command{ |
|
24 |
Use: "notifications", // XXX |
|
25 |
Aliases: []string{"notification", "notif"}, |
|
26 |
Short: "Manage notifications", |
|
27 |
Example: ` madonctl accounts notifications --list |
|
28 |
madonctl accounts notifications --list --clear |
|
19
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
29 |
madonctl accounts notifications --dismiss --notification-id N |
0 | 30 |
madonctl accounts notifications --notification-id N`, |
31 |
//Long: `TBW...`, |
|
32 |
RunE: notificationRunE, |
|
33 |
} |
|
34 |
||
35 |
func init() { |
|
36 |
accountsCmd.AddCommand(notificationsCmd) |
|
37 |
||
38 |
notificationsCmd.Flags().BoolVar(¬ificationsOpts.list, "list", false, "List all current notifications") |
|
39 |
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
|
40 |
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
|
41 |
notificationsCmd.Flags().Int64Var(¬ificationsOpts.notifID, "notification-id", 0, "Get a notification") |
0 | 42 |
} |
43 |
||
44 |
func notificationRunE(cmd *cobra.Command, args []string) error { |
|
45 |
opt := notificationsOpts |
|
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 |
||
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
55 |
var limOpts *madon.LimitParams |
30
14561d44211b
Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
56 |
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
|
57 |
limOpts = new(madon.LimitParams) |
30
14561d44211b
Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
58 |
limOpts.All = accountsOpts.all |
22
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 |
|
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
61 |
if accountsOpts.limit > 0 { |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
62 |
limOpts.Limit = int(accountsOpts.limit) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
63 |
} |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
64 |
if accountsOpts.maxID > 0 { |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
65 |
limOpts.MaxID = int64(accountsOpts.maxID) |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
66 |
} |
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
67 |
if accountsOpts.sinceID > 0 { |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
68 |
limOpts.SinceID = int64(accountsOpts.sinceID) |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
69 |
} |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
70 |
|
0 | 71 |
var obj interface{} |
72 |
var err error |
|
73 |
||
74 |
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
|
75 |
var notifications []madon.Notification |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
76 |
notifications, err = gClient.GetNotifications(limOpts) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
19
diff
changeset
|
77 |
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
|
78 |
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
|
79 |
} |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
80 |
obj = notifications |
0 | 81 |
} else if opt.notifID > 0 { |
19
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
82 |
if opt.dismiss { |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
83 |
err = gClient.DismissNotification(opt.notifID) |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
84 |
} else { |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
85 |
obj, err = gClient.GetNotification(opt.notifID) |
4c81f4393773
Add notification --dismiss to clear a single notification
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
86 |
} |
0 | 87 |
} |
88 |
||
89 |
if err == nil && opt.clear { |
|
90 |
err = gClient.ClearNotifications() |
|
91 |
} |
|
92 |
||
93 |
if err != nil { |
|
94 |
errPrint("Error: %s", err.Error()) |
|
47
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
95 |
os.Exit(1) |
0 | 96 |
} |
97 |
if obj == nil { |
|
98 |
return nil |
|
99 |
} |
|
100 |
||
101 |
p, err := getPrinter() |
|
102 |
if err != nil { |
|
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
103 |
errPrint("Error: %s", err.Error()) |
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
104 |
os.Exit(1) |
0 | 105 |
} |
110 | 106 |
return p.printObj(obj) |
0 | 107 |
} |