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