Add option --text-file to send content from a file
authorMikael Berthe <mikael@lilotux.net>
Mon, 24 Apr 2017 23:05:27 +0200
changeset 15 8ac069eaa817
parent 14 da2059f2fa6a
child 16 8939959991b3
Add option --text-file to send content from a file
cmd/status.go
cmd/toot.go
--- a/cmd/status.go	Mon Apr 24 22:30:07 2017 +0200
+++ b/cmd/status.go	Mon Apr 24 23:05:27 2017 +0200
@@ -7,6 +7,7 @@
 
 import (
 	"errors"
+	"io/ioutil"
 	"strings"
 
 	"github.com/spf13/cobra"
@@ -19,12 +20,13 @@
 	unset    bool
 
 	// The following fields are used for the post/toot command
-	visibility  string
-	sensitive   bool
-	spoiler     string
-	inReplyToID int
-	filePath    string
-	mediaIDs    string
+	visibility    string
+	sensitive     bool
+	spoiler       string
+	inReplyToID   int
+	mediaIDs      string
+	mediaFilePath string
+	textFilePath  string
 
 	// Used for several subcommands to limit the number of results
 	limit uint
@@ -49,7 +51,8 @@
 	statusPostSubcommand.Flags().StringVar(&statusOpts.visibility, "visibility", "", "Visibility (direct|private|unlisted|public)")
 	statusPostSubcommand.Flags().StringVar(&statusOpts.spoiler, "spoiler", "", "Spoiler warning (CW)")
 	statusPostSubcommand.Flags().StringVar(&statusOpts.mediaIDs, "media-ids", "", "Comma-separated list of media IDs")
-	statusPostSubcommand.Flags().StringVarP(&statusOpts.filePath, "file", "f", "", "File name")
+	statusPostSubcommand.Flags().StringVarP(&statusOpts.mediaFilePath, "file", "f", "", "Media file name")
+	statusPostSubcommand.Flags().StringVar(&statusOpts.textFilePath, "text-file", "", "Text file name (message content)")
 	statusPostSubcommand.Flags().IntVarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to")
 }
 
@@ -147,7 +150,8 @@
 	Short:   "Post a message (same as 'madonctl toot')",
 	Example: `  madonctl status post --spoiler Warning "Hello, World"
   madonctl status toot --sensitive --file image.jpg Image
-  madonctl status post --media-ids ID1,ID2,ID3 Image`,
+  madonctl status post --media-ids ID1,ID2,ID3 Image
+  madonctl status toot --text-file message.txt`,
 	RunE: func(cmd *cobra.Command, args []string) error {
 		return statusSubcommandRunE(cmd.Name(), args)
 	},
@@ -202,7 +206,15 @@
 		}
 	case "post": // toot
 		var s *madon.Status
-		s, err = toot(strings.Join(args, " "))
+		text := strings.Join(args, " ")
+		if opt.textFilePath != "" {
+			var b []byte
+			if b, err = ioutil.ReadFile(opt.textFilePath); err != nil {
+				break
+			}
+			text = string(b)
+		}
+		s, err = toot(text)
 		obj = s
 	default:
 		return errors.New("statusSubcommand: internal error")
--- a/cmd/toot.go	Mon Apr 24 22:30:07 2017 +0200
+++ b/cmd/toot.go	Mon Apr 24 23:05:27 2017 +0200
@@ -22,7 +22,8 @@
 	tootAliasCmd.Flags().StringVar(&statusOpts.visibility, "visibility", "", "Visibility (direct|private|unlisted|public)")
 	tootAliasCmd.Flags().StringVar(&statusOpts.spoiler, "spoiler", "", "Spoiler warning (CW)")
 	tootAliasCmd.Flags().StringVar(&statusOpts.mediaIDs, "media-ids", "", "Comma-separated list of media IDs")
-	tootAliasCmd.Flags().StringVarP(&statusOpts.filePath, "file", "f", "", "Media attachment file name")
+	tootAliasCmd.Flags().StringVarP(&statusOpts.mediaFilePath, "file", "f", "", "Media attachment file name")
+	tootAliasCmd.Flags().StringVar(&statusOpts.textFilePath, "text-file", "", "Text file name (message content)")
 	tootAliasCmd.Flags().IntVarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to")
 }
 
@@ -33,7 +34,8 @@
 	Example: `  madonctl toot message
   madonctl toot --spoiler Warning "Hello, World"
   madonctl status post --media-ids ID1,ID2 "Here are the photos"
-  madonctl post --sensitive --file image.jpg Image`,
+  madonctl post --sensitive --file image.jpg Image
+  madonctl post --text-file message.txt`,
 	RunE: func(cmd *cobra.Command, args []string) error {
 		if err := madonInit(true); err != nil {
 			return err
@@ -61,12 +63,12 @@
 		return nil, errors.New("cannot parse media IDs")
 	}
 
-	if opt.filePath != "" {
+	if opt.mediaFilePath != "" {
 		if len(ids) > 3 {
 			return nil, errors.New("too many media attachments")
 		}
 
-		fileMediaID, err := uploadFile(opt.filePath)
+		fileMediaID, err := uploadFile(opt.mediaFilePath)
 		if err != nil {
 			return nil, errors.New("cannot attach media file: " + err.Error())
 		}