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