author | Mikael Berthe <mikael@lilotux.net> |
Mon, 24 Apr 2017 23:05:27 +0200 | |
changeset 15 | 8ac069eaa817 |
parent 13 | f862af8faf17 |
child 20 | b0ccc09f07a2 |
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 |
0 | 33 |
} |
34 |
||
35 |
func init() { |
|
36 |
RootCmd.AddCommand(statusCmd) |
|
37 |
||
38 |
// Subcommands |
|
39 |
statusCmd.AddCommand(statusSubcommands...) |
|
40 |
||
41 |
// Global flags |
|
42 |
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
|
43 |
statusCmd.PersistentFlags().UintVarP(&statusOpts.limit, "limit", "l", 0, "Limit number of results") |
0 | 44 |
|
45 |
statusCmd.MarkPersistentFlagRequired("status-id") |
|
46 |
||
47 |
// Subcommand flags |
|
48 |
statusReblogSubcommand.Flags().BoolVar(&statusOpts.unset, "unset", false, "Unreblog the status") |
|
49 |
statusFavouriteSubcommand.Flags().BoolVar(&statusOpts.unset, "unset", false, "Remove the status from the favourites") |
|
50 |
statusPostSubcommand.Flags().BoolVar(&statusOpts.sensitive, "sensitive", false, "Mark post as sensitive (NSFW)") |
|
51 |
statusPostSubcommand.Flags().StringVar(&statusOpts.visibility, "visibility", "", "Visibility (direct|private|unlisted|public)") |
|
52 |
statusPostSubcommand.Flags().StringVar(&statusOpts.spoiler, "spoiler", "", "Spoiler warning (CW)") |
|
53 |
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
|
54 |
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
|
55 |
statusPostSubcommand.Flags().StringVar(&statusOpts.textFilePath, "text-file", "", "Text file name (message content)") |
0 | 56 |
statusPostSubcommand.Flags().IntVarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to") |
57 |
} |
|
58 |
||
59 |
// statusCmd represents the status command |
|
60 |
// This command does nothing without a subcommand |
|
61 |
var statusCmd = &cobra.Command{ |
|
62 |
Use: "status --status-id ID subcommand", |
|
63 |
Aliases: []string{"st"}, |
|
64 |
Short: "Get status details", |
|
65 |
//Long: `TBW...`, // TODO |
|
66 |
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
|
67 |
// This is common to status and all status subcommands but "post" |
|
68 |
if statusOpts.statusID < 1 && cmd.Name() != "post" { |
|
69 |
return errors.New("missing status ID") |
|
70 |
} |
|
71 |
if err := madonInit(true); err != nil { |
|
72 |
return err |
|
73 |
} |
|
74 |
return nil |
|
75 |
}, |
|
76 |
} |
|
77 |
||
78 |
var statusSubcommands = []*cobra.Command{ |
|
79 |
&cobra.Command{ |
|
80 |
Use: "show", |
|
81 |
Aliases: []string{"display"}, |
|
82 |
Short: "Get the status", |
|
83 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
84 |
return statusSubcommandRunE(cmd.Name(), args) |
|
85 |
}, |
|
86 |
}, |
|
87 |
&cobra.Command{ |
|
88 |
Use: "context", |
|
89 |
Short: "Get the status context", |
|
90 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
91 |
return statusSubcommandRunE(cmd.Name(), args) |
|
92 |
}, |
|
93 |
}, |
|
94 |
&cobra.Command{ |
|
95 |
Use: "card", |
|
96 |
Short: "Get the status card", |
|
97 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
98 |
return statusSubcommandRunE(cmd.Name(), args) |
|
99 |
}, |
|
100 |
}, |
|
101 |
&cobra.Command{ |
|
102 |
Use: "reblogged-by", |
|
103 |
Short: "Display accounts which reblogged the status", |
|
104 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
105 |
return statusSubcommandRunE(cmd.Name(), args) |
|
106 |
}, |
|
107 |
}, |
|
108 |
&cobra.Command{ |
|
109 |
Use: "favourited-by", |
|
110 |
Aliases: []string{"favorited-by"}, |
|
111 |
Short: "Display accounts which favourited the status", |
|
112 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
113 |
return statusSubcommandRunE(cmd.Name(), args) |
|
114 |
}, |
|
115 |
}, |
|
116 |
&cobra.Command{ |
|
117 |
Use: "delete", |
|
118 |
Aliases: []string{"rm"}, |
|
119 |
Short: "Delete the status", |
|
120 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
121 |
return statusSubcommandRunE(cmd.Name(), args) |
|
122 |
}, |
|
123 |
}, |
|
124 |
statusReblogSubcommand, |
|
125 |
statusFavouriteSubcommand, |
|
126 |
statusPostSubcommand, |
|
127 |
} |
|
128 |
||
129 |
var statusReblogSubcommand = &cobra.Command{ |
|
130 |
Use: "boost", |
|
131 |
Aliases: []string{"reblog"}, |
|
132 |
Short: "Boost (reblog) or unreblog the status", |
|
133 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
134 |
return statusSubcommandRunE(cmd.Name(), args) |
|
135 |
}, |
|
136 |
} |
|
137 |
||
138 |
var statusFavouriteSubcommand = &cobra.Command{ |
|
139 |
Use: "favourite", |
|
140 |
Aliases: []string{"favorite", "fave"}, |
|
141 |
Short: "Mark/unmark the status as favourite", |
|
142 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
143 |
return statusSubcommandRunE(cmd.Name(), args) |
|
144 |
}, |
|
145 |
} |
|
146 |
||
147 |
var statusPostSubcommand = &cobra.Command{ |
|
148 |
Use: "post", |
|
149 |
Aliases: []string{"toot", "pouet"}, |
|
150 |
Short: "Post a message (same as 'madonctl toot')", |
|
151 |
Example: ` madonctl status post --spoiler Warning "Hello, World" |
|
152 |
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
|
153 |
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
|
154 |
madonctl status toot --text-file message.txt`, |
0 | 155 |
RunE: func(cmd *cobra.Command, args []string) error { |
156 |
return statusSubcommandRunE(cmd.Name(), args) |
|
157 |
}, |
|
158 |
} |
|
159 |
||
160 |
func statusSubcommandRunE(subcmd string, args []string) error { |
|
161 |
opt := statusOpts |
|
162 |
||
163 |
var obj interface{} |
|
164 |
var err error |
|
165 |
||
166 |
switch subcmd { |
|
167 |
case "show": |
|
168 |
var status *madon.Status |
|
169 |
status, err = gClient.GetStatus(opt.statusID) |
|
170 |
obj = status |
|
171 |
case "context": |
|
172 |
var context *madon.Context |
|
173 |
context, err = gClient.GetStatusContext(opt.statusID) |
|
174 |
obj = context |
|
175 |
case "card": |
|
176 |
var context *madon.Card |
|
177 |
context, err = gClient.GetStatusCard(opt.statusID) |
|
178 |
obj = context |
|
179 |
case "reblogged-by": |
|
180 |
var accountList []madon.Account |
|
181 |
accountList, err = gClient.GetStatusRebloggedBy(opt.statusID) |
|
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
182 |
if opt.limit > 0 { |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
183 |
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
|
184 |
} |
0 | 185 |
obj = accountList |
186 |
case "favourited-by": |
|
187 |
var accountList []madon.Account |
|
188 |
accountList, err = gClient.GetStatusFavouritedBy(opt.statusID) |
|
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
189 |
if opt.limit > 0 { |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
190 |
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
|
191 |
} |
0 | 192 |
obj = accountList |
193 |
case "delete": |
|
194 |
err = gClient.DeleteStatus(opt.statusID) |
|
195 |
case "boost": |
|
196 |
if opt.unset { |
|
197 |
err = gClient.UnreblogStatus(opt.statusID) |
|
198 |
} else { |
|
199 |
err = gClient.ReblogStatus(opt.statusID) |
|
200 |
} |
|
201 |
case "favourite": |
|
202 |
if opt.unset { |
|
203 |
err = gClient.UnfavouriteStatus(opt.statusID) |
|
204 |
} else { |
|
205 |
err = gClient.FavouriteStatus(opt.statusID) |
|
206 |
} |
|
207 |
case "post": // toot |
|
208 |
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
|
209 |
text := strings.Join(args, " ") |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
210 |
if opt.textFilePath != "" { |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
211 |
var b []byte |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
212 |
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
|
213 |
break |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
214 |
} |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
215 |
text = string(b) |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
216 |
} |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
217 |
s, err = toot(text) |
0 | 218 |
obj = s |
219 |
default: |
|
220 |
return errors.New("statusSubcommand: internal error") |
|
221 |
} |
|
222 |
||
223 |
if err != nil { |
|
224 |
errPrint("Error: %s", err.Error()) |
|
225 |
return nil |
|
226 |
} |
|
227 |
if obj == nil { |
|
228 |
return nil |
|
229 |
} |
|
230 |
||
231 |
p, err := getPrinter() |
|
232 |
if err != nil { |
|
233 |
return err |
|
234 |
} |
|
235 |
return p.PrintObj(obj, nil, "") |
|
236 |
} |