author | Mikael Berthe <mikael@lilotux.net> |
Wed, 10 May 2017 20:13:31 +0200 | |
changeset 116 | 3dd35bdb63ac |
parent 79 | bca27c55be9f |
child 129 | 11966471aac3 |
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" |
79
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
11 |
"github.com/spf13/viper" |
0 | 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)") |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
35
diff
changeset
|
27 |
tootAliasCmd.Flags().Int64VarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to") |
71
67e3234c26a9
Add --stdin to send a message from standard input
Mikael Berthe <mikael@lilotux.net>
parents:
70
diff
changeset
|
28 |
tootAliasCmd.Flags().BoolVar(&statusOpts.stdin, "stdin", false, "Read message content from standard input") |
35
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
29 |
|
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
30 |
// Flag completion |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
31 |
annotation := make(map[string][]string) |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
32 |
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
|
33 |
|
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
17
diff
changeset
|
34 |
tootAliasCmd.Flags().Lookup("visibility").Annotations = annotation |
0 | 35 |
} |
36 |
||
37 |
var tootAliasCmd = &cobra.Command{ |
|
38 |
Use: "toot", |
|
39 |
Aliases: []string{"post", "pouet"}, |
|
40 |
Short: "Post a message (toot)", |
|
41 |
Example: ` madonctl toot message |
|
42 |
madonctl toot --spoiler Warning "Hello, World" |
|
43 |
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
|
44 |
madonctl post --sensitive --file image.jpg Image |
71
67e3234c26a9
Add --stdin to send a message from standard input
Mikael Berthe <mikael@lilotux.net>
parents:
70
diff
changeset
|
45 |
madonctl toot --text-file message.txt |
79
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
46 |
echo "Hello from #madonctl" | madonctl toot --visibility unlisted --stdin |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
47 |
|
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
48 |
The default visibility can be set in the configuration file with the option |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
49 |
'default_visibility' (or with an environmnent variable).`, |
0 | 50 |
RunE: func(cmd *cobra.Command, args []string) error { |
51 |
if err := madonInit(true); err != nil { |
|
52 |
return err |
|
53 |
} |
|
54 |
return statusSubcommandRunE("post", args) |
|
55 |
}, |
|
56 |
} |
|
57 |
||
58 |
func toot(tootText string) (*madon.Status, error) { |
|
59 |
opt := statusOpts |
|
60 |
||
79
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
61 |
// Get default visibility from configuration |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
62 |
if opt.visibility == "" { |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
63 |
if v := viper.GetString("default_visibility"); v != "" { |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
64 |
opt.visibility = v |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
65 |
} |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
66 |
} |
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
67 |
|
0 | 68 |
switch opt.visibility { |
69 |
case "", "direct", "private", "unlisted", "public": |
|
70 |
// OK |
|
71 |
default: |
|
79
bca27c55be9f
Add default visibility setting
Mikael Berthe <mikael@lilotux.net>
parents:
71
diff
changeset
|
72 |
return nil, errors.Errorf("invalid visibility argument value '%s'", opt.visibility) |
0 | 73 |
} |
74 |
||
75 |
if opt.inReplyToID < 0 { |
|
76 |
return nil, errors.New("invalid in-reply-to argument value") |
|
77 |
} |
|
78 |
||
79 |
ids, err := splitIDs(opt.mediaIDs) |
|
80 |
if err != nil { |
|
81 |
return nil, errors.New("cannot parse media IDs") |
|
82 |
} |
|
83 |
||
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
84 |
if opt.mediaFilePath != "" { |
0 | 85 |
if len(ids) > 3 { |
86 |
return nil, errors.New("too many media attachments") |
|
87 |
} |
|
88 |
||
15
8ac069eaa817
Add option --text-file to send content from a file
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
89 |
fileMediaID, err := uploadFile(opt.mediaFilePath) |
0 | 90 |
if err != nil { |
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
91 |
return nil, errors.Wrap(err, "cannot attach media file") |
0 | 92 |
} |
93 |
if fileMediaID > 0 { |
|
94 |
ids = append(ids, fileMediaID) |
|
95 |
} |
|
96 |
} |
|
97 |
||
70
155fdea254c3
Do not make an API call if toot has no content
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
98 |
if tootText == "" && len(ids) == 0 && opt.spoiler == "" { |
155fdea254c3
Do not make an API call if toot has no content
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
99 |
return nil, errors.New("toot is empty") |
155fdea254c3
Do not make an API call if toot has no content
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
100 |
} |
155fdea254c3
Do not make an API call if toot has no content
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
101 |
|
0 | 102 |
return gClient.PostStatus(tootText, opt.inReplyToID, ids, opt.sensitive, opt.spoiler, opt.visibility) |
103 |
} |