author | convert-repo |
Fri, 05 May 2017 21:55:09 +0000 | |
changeset 69 | 7217174c217a |
parent 47 | 82d8b6074309 |
child 71 | 67e3234c26a9 |
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 ( |
|
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
9 |
"io/ioutil" |
47
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
10 |
"os" |
0 | 11 |
"strings" |
12 |
||
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
13 |
"github.com/pkg/errors" |
0 | 14 |
"github.com/spf13/cobra" |
15 |
||
16 |
"github.com/McKael/madon" |
|
17 |
) |
|
18 |
||
19 |
var statusOpts struct { |
|
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
35
diff
changeset
|
20 |
statusID int64 |
0 | 21 |
unset bool |
22 |
||
23 |
// The following fields are used for the post/toot command |
|
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
24 |
visibility string |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
25 |
sensitive bool |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
26 |
spoiler string |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
35
diff
changeset
|
27 |
inReplyToID int64 |
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
28 |
mediaIDs string |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
29 |
mediaFilePath string |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
30 |
textFilePath string |
0 | 31 |
|
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
32 |
// Used for several subcommands to limit the number of results |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
33 |
limit uint |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
35
diff
changeset
|
34 |
//sinceID, maxID int64 |
30
14561d44211b
Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
35 |
all bool |
0 | 36 |
} |
37 |
||
38 |
func init() { |
|
39 |
RootCmd.AddCommand(statusCmd) |
|
40 |
||
41 |
// Subcommands |
|
42 |
statusCmd.AddCommand(statusSubcommands...) |
|
43 |
||
44 |
// Global flags |
|
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
35
diff
changeset
|
45 |
statusCmd.PersistentFlags().Int64VarP(&statusOpts.statusID, "status-id", "s", 0, "Status ID number") |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
46 |
statusCmd.PersistentFlags().UintVarP(&statusOpts.limit, "limit", "l", 0, "Limit number of results") |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
35
diff
changeset
|
47 |
//statusCmd.PersistentFlags().Int64Var(&statusOpts.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:
35
diff
changeset
|
48 |
//statusCmd.PersistentFlags().Int64Var(&statusOpts.maxID, "max-id", 0, "Request IDs less (or equal) than a value") |
30
14561d44211b
Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
49 |
statusCmd.PersistentFlags().BoolVar(&statusOpts.all, "all", false, "Fetch all results (for reblogged-by/favourited-by)") |
0 | 50 |
|
51 |
statusCmd.MarkPersistentFlagRequired("status-id") |
|
52 |
||
53 |
// Subcommand flags |
|
54 |
statusReblogSubcommand.Flags().BoolVar(&statusOpts.unset, "unset", false, "Unreblog the status") |
|
55 |
statusFavouriteSubcommand.Flags().BoolVar(&statusOpts.unset, "unset", false, "Remove the status from the favourites") |
|
56 |
statusPostSubcommand.Flags().BoolVar(&statusOpts.sensitive, "sensitive", false, "Mark post as sensitive (NSFW)") |
|
57 |
statusPostSubcommand.Flags().StringVar(&statusOpts.visibility, "visibility", "", "Visibility (direct|private|unlisted|public)") |
|
58 |
statusPostSubcommand.Flags().StringVar(&statusOpts.spoiler, "spoiler", "", "Spoiler warning (CW)") |
|
59 |
statusPostSubcommand.Flags().StringVar(&statusOpts.mediaIDs, "media-ids", "", "Comma-separated list of media IDs") |
|
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
60 |
statusPostSubcommand.Flags().StringVarP(&statusOpts.mediaFilePath, "file", "f", "", "Media file name") |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
61 |
statusPostSubcommand.Flags().StringVar(&statusOpts.textFilePath, "text-file", "", "Text file name (message content)") |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
35
diff
changeset
|
62 |
statusPostSubcommand.Flags().Int64VarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to") |
35
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
63 |
|
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
64 |
// Flag completion |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
65 |
annotation := make(map[string][]string) |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
66 |
annotation[cobra.BashCompCustom] = []string{"__madonctl_visibility"} |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
67 |
|
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
68 |
statusPostSubcommand.Flags().Lookup("visibility").Annotations = annotation |
0 | 69 |
} |
70 |
||
71 |
// statusCmd represents the status command |
|
72 |
// This command does nothing without a subcommand |
|
73 |
var statusCmd = &cobra.Command{ |
|
74 |
Use: "status --status-id ID subcommand", |
|
75 |
Aliases: []string{"st"}, |
|
76 |
Short: "Get status details", |
|
77 |
//Long: `TBW...`, // TODO |
|
78 |
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
|
79 |
// This is common to status and all status subcommands but "post" |
|
80 |
if statusOpts.statusID < 1 && cmd.Name() != "post" { |
|
81 |
return errors.New("missing status ID") |
|
82 |
} |
|
83 |
if err := madonInit(true); err != nil { |
|
84 |
return err |
|
85 |
} |
|
86 |
return nil |
|
87 |
}, |
|
88 |
} |
|
89 |
||
90 |
var statusSubcommands = []*cobra.Command{ |
|
91 |
&cobra.Command{ |
|
92 |
Use: "show", |
|
93 |
Aliases: []string{"display"}, |
|
94 |
Short: "Get the status", |
|
95 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
96 |
return statusSubcommandRunE(cmd.Name(), args) |
|
97 |
}, |
|
98 |
}, |
|
99 |
&cobra.Command{ |
|
100 |
Use: "context", |
|
101 |
Short: "Get the status context", |
|
102 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
103 |
return statusSubcommandRunE(cmd.Name(), args) |
|
104 |
}, |
|
105 |
}, |
|
106 |
&cobra.Command{ |
|
107 |
Use: "card", |
|
108 |
Short: "Get the status card", |
|
109 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
110 |
return statusSubcommandRunE(cmd.Name(), args) |
|
111 |
}, |
|
112 |
}, |
|
113 |
&cobra.Command{ |
|
114 |
Use: "reblogged-by", |
|
115 |
Short: "Display accounts which reblogged the status", |
|
116 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
117 |
return statusSubcommandRunE(cmd.Name(), args) |
|
118 |
}, |
|
119 |
}, |
|
120 |
&cobra.Command{ |
|
121 |
Use: "favourited-by", |
|
122 |
Aliases: []string{"favorited-by"}, |
|
123 |
Short: "Display accounts which favourited the status", |
|
124 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
125 |
return statusSubcommandRunE(cmd.Name(), args) |
|
126 |
}, |
|
127 |
}, |
|
128 |
&cobra.Command{ |
|
129 |
Use: "delete", |
|
130 |
Aliases: []string{"rm"}, |
|
131 |
Short: "Delete the status", |
|
132 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
133 |
return statusSubcommandRunE(cmd.Name(), args) |
|
134 |
}, |
|
135 |
}, |
|
136 |
statusReblogSubcommand, |
|
137 |
statusFavouriteSubcommand, |
|
138 |
statusPostSubcommand, |
|
139 |
} |
|
140 |
||
141 |
var statusReblogSubcommand = &cobra.Command{ |
|
142 |
Use: "boost", |
|
143 |
Aliases: []string{"reblog"}, |
|
144 |
Short: "Boost (reblog) or unreblog the status", |
|
145 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
146 |
return statusSubcommandRunE(cmd.Name(), args) |
|
147 |
}, |
|
148 |
} |
|
149 |
||
150 |
var statusFavouriteSubcommand = &cobra.Command{ |
|
151 |
Use: "favourite", |
|
152 |
Aliases: []string{"favorite", "fave"}, |
|
153 |
Short: "Mark/unmark the status as favourite", |
|
154 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
155 |
return statusSubcommandRunE(cmd.Name(), args) |
|
156 |
}, |
|
157 |
} |
|
158 |
||
159 |
var statusPostSubcommand = &cobra.Command{ |
|
160 |
Use: "post", |
|
161 |
Aliases: []string{"toot", "pouet"}, |
|
162 |
Short: "Post a message (same as 'madonctl toot')", |
|
163 |
Example: ` madonctl status post --spoiler Warning "Hello, World" |
|
164 |
madonctl status toot --sensitive --file image.jpg Image |
|
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
165 |
madonctl status post --media-ids ID1,ID2,ID3 Image |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
166 |
madonctl status toot --text-file message.txt`, |
0 | 167 |
RunE: func(cmd *cobra.Command, args []string) error { |
168 |
return statusSubcommandRunE(cmd.Name(), args) |
|
169 |
}, |
|
170 |
} |
|
171 |
||
172 |
func statusSubcommandRunE(subcmd string, args []string) error { |
|
173 |
opt := statusOpts |
|
174 |
||
175 |
var obj interface{} |
|
176 |
var err error |
|
177 |
||
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
15
diff
changeset
|
178 |
var limOpts *madon.LimitParams |
30
14561d44211b
Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
179 |
if opt.all || opt.limit > 0 /* || opt.sinceID > 0 || opt.maxID > 0 */ { |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
180 |
limOpts = new(madon.LimitParams) |
30
14561d44211b
Add option --all to fetch all available results
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
181 |
limOpts.All = opt.all |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
182 |
} |
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
183 |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
15
diff
changeset
|
184 |
if opt.limit > 0 { |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
15
diff
changeset
|
185 |
limOpts.Limit = int(opt.limit) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
15
diff
changeset
|
186 |
} |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
187 |
/* |
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
188 |
if opt.maxID > 0 { |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
35
diff
changeset
|
189 |
limOpts.MaxID = int64(opt.maxID) |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
190 |
} |
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
191 |
if opt.sinceID > 0 { |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
35
diff
changeset
|
192 |
limOpts.SinceID = int64(opt.sinceID) |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
193 |
} |
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
194 |
*/ |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
15
diff
changeset
|
195 |
|
0 | 196 |
switch subcmd { |
197 |
case "show": |
|
198 |
var status *madon.Status |
|
199 |
status, err = gClient.GetStatus(opt.statusID) |
|
200 |
obj = status |
|
201 |
case "context": |
|
202 |
var context *madon.Context |
|
203 |
context, err = gClient.GetStatusContext(opt.statusID) |
|
204 |
obj = context |
|
205 |
case "card": |
|
206 |
var context *madon.Card |
|
207 |
context, err = gClient.GetStatusCard(opt.statusID) |
|
208 |
obj = context |
|
209 |
case "reblogged-by": |
|
210 |
var accountList []madon.Account |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
15
diff
changeset
|
211 |
accountList, err = gClient.GetStatusRebloggedBy(opt.statusID, limOpts) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
15
diff
changeset
|
212 |
if opt.limit > 0 && len(accountList) > 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
|
213 |
accountList = accountList[:opt.limit] |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
214 |
} |
0 | 215 |
obj = accountList |
216 |
case "favourited-by": |
|
217 |
var accountList []madon.Account |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
15
diff
changeset
|
218 |
accountList, err = gClient.GetStatusFavouritedBy(opt.statusID, limOpts) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
15
diff
changeset
|
219 |
if opt.limit > 0 && len(accountList) > 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
|
220 |
accountList = accountList[:opt.limit] |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
221 |
} |
0 | 222 |
obj = accountList |
223 |
case "delete": |
|
224 |
err = gClient.DeleteStatus(opt.statusID) |
|
225 |
case "boost": |
|
226 |
if opt.unset { |
|
227 |
err = gClient.UnreblogStatus(opt.statusID) |
|
228 |
} else { |
|
229 |
err = gClient.ReblogStatus(opt.statusID) |
|
230 |
} |
|
231 |
case "favourite": |
|
232 |
if opt.unset { |
|
233 |
err = gClient.UnfavouriteStatus(opt.statusID) |
|
234 |
} else { |
|
235 |
err = gClient.FavouriteStatus(opt.statusID) |
|
236 |
} |
|
237 |
case "post": // toot |
|
238 |
var s *madon.Status |
|
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
239 |
text := strings.Join(args, " ") |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
240 |
if opt.textFilePath != "" { |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
241 |
var b []byte |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
242 |
if b, err = ioutil.ReadFile(opt.textFilePath); err != nil { |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
243 |
break |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
244 |
} |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
245 |
text = string(b) |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
246 |
} |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
247 |
s, err = toot(text) |
0 | 248 |
obj = s |
249 |
default: |
|
250 |
return errors.New("statusSubcommand: internal error") |
|
251 |
} |
|
252 |
||
253 |
if err != nil { |
|
254 |
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
|
255 |
os.Exit(1) |
0 | 256 |
} |
257 |
if obj == nil { |
|
258 |
return nil |
|
259 |
} |
|
260 |
||
261 |
p, err := getPrinter() |
|
262 |
if err != nil { |
|
263 |
return err |
|
264 |
} |
|
265 |
return p.PrintObj(obj, nil, "") |
|
266 |
} |