author | convert-repo |
Fri, 05 May 2017 21:55:09 +0000 | |
changeset 69 | 7217174c217a |
parent 45 | b58a7ea1aeb2 |
child 70 | 155fdea254c3 |
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 ( |
|
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
9 |
"github.com/pkg/errors" |
0 | 10 |
"github.com/spf13/cobra" |
11 |
||
12 |
"github.com/McKael/madon" |
|
13 |
) |
|
14 |
||
15 |
// toot is a kind of alias for status post |
|
16 |
||
17 |
func init() { |
|
18 |
RootCmd.AddCommand(tootAliasCmd) |
|
19 |
||
20 |
tootAliasCmd.Flags().BoolVar(&statusOpts.sensitive, "sensitive", false, "Mark post as sensitive (NSFW)") |
|
21 |
tootAliasCmd.Flags().StringVar(&statusOpts.visibility, "visibility", "", "Visibility (direct|private|unlisted|public)") |
|
22 |
tootAliasCmd.Flags().StringVar(&statusOpts.spoiler, "spoiler", "", "Spoiler warning (CW)") |
|
23 |
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
|
24 |
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
|
25 |
tootAliasCmd.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
|
26 |
tootAliasCmd.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:
17
diff
changeset
|
27 |
|
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
28 |
// Flag completion |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
29 |
annotation := make(map[string][]string) |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
30 |
annotation[cobra.BashCompCustom] = []string{"__madonctl_visibility"} |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
31 |
|
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
32 |
tootAliasCmd.Flags().Lookup("visibility").Annotations = annotation |
0 | 33 |
} |
34 |
||
35 |
var tootAliasCmd = &cobra.Command{ |
|
36 |
Use: "toot", |
|
37 |
Aliases: []string{"post", "pouet"}, |
|
38 |
Short: "Post a message (toot)", |
|
39 |
Example: ` madonctl toot message |
|
40 |
madonctl toot --spoiler Warning "Hello, World" |
|
41 |
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
|
42 |
madonctl post --sensitive --file image.jpg Image |
17 | 43 |
madonctl toot --text-file message.txt`, |
0 | 44 |
RunE: func(cmd *cobra.Command, args []string) error { |
45 |
if err := madonInit(true); err != nil { |
|
46 |
return err |
|
47 |
} |
|
48 |
return statusSubcommandRunE("post", args) |
|
49 |
}, |
|
50 |
} |
|
51 |
||
52 |
func toot(tootText string) (*madon.Status, error) { |
|
53 |
opt := statusOpts |
|
54 |
||
55 |
switch opt.visibility { |
|
56 |
case "", "direct", "private", "unlisted", "public": |
|
57 |
// OK |
|
58 |
default: |
|
59 |
return nil, errors.New("invalid visibility argument value") |
|
60 |
} |
|
61 |
||
62 |
if opt.inReplyToID < 0 { |
|
63 |
return nil, errors.New("invalid in-reply-to argument value") |
|
64 |
} |
|
65 |
||
66 |
ids, err := splitIDs(opt.mediaIDs) |
|
67 |
if err != nil { |
|
68 |
return nil, errors.New("cannot parse media IDs") |
|
69 |
} |
|
70 |
||
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
71 |
if opt.mediaFilePath != "" { |
0 | 72 |
if len(ids) > 3 { |
73 |
return nil, errors.New("too many media attachments") |
|
74 |
} |
|
75 |
||
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
76 |
fileMediaID, err := uploadFile(opt.mediaFilePath) |
0 | 77 |
if err != nil { |
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
78 |
return nil, errors.Wrap(err, "cannot attach media file") |
0 | 79 |
} |
80 |
if fileMediaID > 0 { |
|
81 |
ids = append(ids, fileMediaID) |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
return gClient.PostStatus(tootText, opt.inReplyToID, ids, opt.sensitive, opt.spoiler, opt.visibility) |
|
86 |
} |