cmd/toot.go
changeset 0 5abace724584
child 15 8ac069eaa817
equal deleted inserted replaced
-1:000000000000 0:5abace724584
       
     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")
       
    25 	tootAliasCmd.Flags().StringVarP(&statusOpts.filePath, "file", "f", "", "Media attachment file name")
       
    26 	tootAliasCmd.Flags().IntVarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to")
       
    27 }
       
    28 
       
    29 var tootAliasCmd = &cobra.Command{
       
    30 	Use:     "toot",
       
    31 	Aliases: []string{"post", "pouet"},
       
    32 	Short:   "Post a message (toot)",
       
    33 	Example: `  madonctl toot message
       
    34   madonctl toot --spoiler Warning "Hello, World"
       
    35   madonctl status post --media-ids ID1,ID2 "Here are the photos"
       
    36   madonctl post --sensitive --file image.jpg Image`,
       
    37 	RunE: func(cmd *cobra.Command, args []string) error {
       
    38 		if err := madonInit(true); err != nil {
       
    39 			return err
       
    40 		}
       
    41 		return statusSubcommandRunE("post", args)
       
    42 	},
       
    43 }
       
    44 
       
    45 func toot(tootText string) (*madon.Status, error) {
       
    46 	opt := statusOpts
       
    47 
       
    48 	switch opt.visibility {
       
    49 	case "", "direct", "private", "unlisted", "public":
       
    50 		// OK
       
    51 	default:
       
    52 		return nil, errors.New("invalid visibility argument value")
       
    53 	}
       
    54 
       
    55 	if opt.inReplyToID < 0 {
       
    56 		return nil, errors.New("invalid in-reply-to argument value")
       
    57 	}
       
    58 
       
    59 	ids, err := splitIDs(opt.mediaIDs)
       
    60 	if err != nil {
       
    61 		return nil, errors.New("cannot parse media IDs")
       
    62 	}
       
    63 
       
    64 	if opt.filePath != "" {
       
    65 		if len(ids) > 3 {
       
    66 			return nil, errors.New("too many media attachments")
       
    67 		}
       
    68 
       
    69 		fileMediaID, err := uploadFile(opt.filePath)
       
    70 		if err != nil {
       
    71 			return nil, errors.New("cannot attach media file: " + err.Error())
       
    72 		}
       
    73 		if fileMediaID > 0 {
       
    74 			ids = append(ids, fileMediaID)
       
    75 		}
       
    76 	}
       
    77 
       
    78 	return gClient.PostStatus(tootText, opt.inReplyToID, ids, opt.sensitive, opt.spoiler, opt.visibility)
       
    79 }