author | Mikael Berthe <mikael@lilotux.net> |
Sun, 07 May 2017 16:43:51 +0200 | |
changeset 87 | cd1c3f1610e4 |
parent 81 | b1671f83e91b |
child 110 | 57843255fd1a |
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:
44
diff
changeset
|
9 |
"os" |
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
10 |
|
0 | 11 |
"github.com/spf13/cobra" |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
12 |
|
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
13 |
"github.com/McKael/madon" |
0 | 14 |
) |
15 |
||
16 |
var timelineOpts struct { |
|
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
17 |
local bool |
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
18 |
limit uint |
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
19 |
sinceID, maxID int64 |
0 | 20 |
} |
21 |
||
22 |
// timelineCmd represents the timelines command |
|
23 |
var timelineCmd = &cobra.Command{ |
|
24 |
Use: "timeline [home|public|:HASHTAG] [--local]", |
|
25 |
Aliases: []string{"tl"}, |
|
26 |
Short: "Fetch a timeline", |
|
27 |
Long: ` |
|
28 |
The timeline command fetches a timeline (home, local or federated). |
|
29 |
It can also get a hashtag-based timeline if the keyword or prefixed with |
|
30 |
':' or '#'.`, |
|
31 |
Example: ` madonctl timeline |
|
32 |
madonctl timeline public --local |
|
33 |
madonctl timeline :mastodon`, |
|
34 |
RunE: timelineRunE, |
|
35 |
ValidArgs: []string{"home", "public"}, |
|
36 |
} |
|
37 |
||
38 |
func init() { |
|
39 |
RootCmd.AddCommand(timelineCmd) |
|
40 |
||
41 |
timelineCmd.Flags().BoolVar(&timelineOpts.local, "local", false, "Posts from the local instance") |
|
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
42 |
timelineCmd.Flags().UintVarP(&timelineOpts.limit, "limit", "l", 0, "Limit number of results") |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
43 |
timelineCmd.PersistentFlags().Int64Var(&timelineOpts.sinceID, "since-id", 0, "Request IDs greater than a value") |
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
44 |
timelineCmd.PersistentFlags().Int64Var(&timelineOpts.maxID, "max-id", 0, "Request IDs less (or equal) than a value") |
0 | 45 |
} |
46 |
||
47 |
func timelineRunE(cmd *cobra.Command, args []string) error { |
|
48 |
opt := timelineOpts |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
49 |
var limOpts *madon.LimitParams |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
50 |
|
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
51 |
if opt.limit > 0 || opt.sinceID > 0 || opt.maxID > 0 { |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
52 |
limOpts = new(madon.LimitParams) |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
53 |
} |
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
54 |
|
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
55 |
if opt.limit > 0 { |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
56 |
limOpts.Limit = int(opt.limit) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
57 |
} |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
58 |
if opt.maxID > 0 { |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
59 |
limOpts.MaxID = opt.maxID |
22
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 opt.sinceID > 0 { |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
62 |
limOpts.SinceID = opt.sinceID |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
63 |
} |
0 | 64 |
|
65 |
tl := "home" |
|
66 |
if len(args) > 0 { |
|
67 |
tl = args[0] |
|
68 |
} |
|
69 |
||
70 |
// The home timeline is the only one requiring to be logged in |
|
71 |
if err := madonInit(tl == "home"); err != nil { |
|
72 |
return err |
|
73 |
} |
|
74 |
||
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
75 |
sl, err := gClient.GetTimelines(tl, opt.local, limOpts) |
0 | 76 |
if err != nil { |
77 |
errPrint("Error: %s", err.Error()) |
|
47
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
78 |
os.Exit(1) |
0 | 79 |
} |
80 |
||
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
81 |
if opt.limit > 0 && len(sl) > int(opt.limit) { |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
82 |
sl = sl[:opt.limit] |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
83 |
} |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
84 |
|
0 | 85 |
p, err := getPrinter() |
86 |
if err != nil { |
|
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
87 |
errPrint("Error: %s", err.Error()) |
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
88 |
os.Exit(1) |
0 | 89 |
} |
90 |
return p.PrintObj(sl, nil, "") |
|
91 |
} |