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