author | Mikael Berthe <mikael@lilotux.net> |
Mon, 24 Apr 2017 23:05:27 +0200 | |
changeset 15 | 8ac069eaa817 |
parent 0 | 5abace724584 |
child 17 | ab1aa9652791 |
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 |
||
11 |
"github.com/spf13/cobra" |
|
12 |
||
13 |
"github.com/McKael/madon" |
|
14 |
) |
|
15 |
||
16 |
// toot is a kind of alias for status post |
|
17 |
||
18 |
func init() { |
|
19 |
RootCmd.AddCommand(tootAliasCmd) |
|
20 |
||
21 |
tootAliasCmd.Flags().BoolVar(&statusOpts.sensitive, "sensitive", false, "Mark post as sensitive (NSFW)") |
|
22 |
tootAliasCmd.Flags().StringVar(&statusOpts.visibility, "visibility", "", "Visibility (direct|private|unlisted|public)") |
|
23 |
tootAliasCmd.Flags().StringVar(&statusOpts.spoiler, "spoiler", "", "Spoiler warning (CW)") |
|
24 |
tootAliasCmd.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:
0
diff
changeset
|
25 |
tootAliasCmd.Flags().StringVarP(&statusOpts.mediaFilePath, "file", "f", "", "Media attachment file name") |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
26 |
tootAliasCmd.Flags().StringVar(&statusOpts.textFilePath, "text-file", "", "Text file name (message content)") |
0 | 27 |
tootAliasCmd.Flags().IntVarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to") |
28 |
} |
|
29 |
||
30 |
var tootAliasCmd = &cobra.Command{ |
|
31 |
Use: "toot", |
|
32 |
Aliases: []string{"post", "pouet"}, |
|
33 |
Short: "Post a message (toot)", |
|
34 |
Example: ` madonctl toot message |
|
35 |
madonctl toot --spoiler Warning "Hello, World" |
|
36 |
madonctl status post --media-ids ID1,ID2 "Here are the photos" |
|
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
37 |
madonctl post --sensitive --file image.jpg Image |
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
38 |
madonctl post --text-file message.txt`, |
0 | 39 |
RunE: func(cmd *cobra.Command, args []string) error { |
40 |
if err := madonInit(true); err != nil { |
|
41 |
return err |
|
42 |
} |
|
43 |
return statusSubcommandRunE("post", args) |
|
44 |
}, |
|
45 |
} |
|
46 |
||
47 |
func toot(tootText string) (*madon.Status, error) { |
|
48 |
opt := statusOpts |
|
49 |
||
50 |
switch opt.visibility { |
|
51 |
case "", "direct", "private", "unlisted", "public": |
|
52 |
// OK |
|
53 |
default: |
|
54 |
return nil, errors.New("invalid visibility argument value") |
|
55 |
} |
|
56 |
||
57 |
if opt.inReplyToID < 0 { |
|
58 |
return nil, errors.New("invalid in-reply-to argument value") |
|
59 |
} |
|
60 |
||
61 |
ids, err := splitIDs(opt.mediaIDs) |
|
62 |
if err != nil { |
|
63 |
return nil, errors.New("cannot parse media IDs") |
|
64 |
} |
|
65 |
||
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
66 |
if opt.mediaFilePath != "" { |
0 | 67 |
if len(ids) > 3 { |
68 |
return nil, errors.New("too many media attachments") |
|
69 |
} |
|
70 |
||
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
71 |
fileMediaID, err := uploadFile(opt.mediaFilePath) |
0 | 72 |
if err != nil { |
73 |
return nil, errors.New("cannot attach media file: " + err.Error()) |
|
74 |
} |
|
75 |
if fileMediaID > 0 { |
|
76 |
ids = append(ids, fileMediaID) |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
return gClient.PostStatus(tootText, opt.inReplyToID, ids, opt.sensitive, opt.spoiler, opt.visibility) |
|
81 |
} |