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