Add --stdin to send a message from standard input
authorMikael Berthe <mikael@lilotux.net>
Sat, 06 May 2017 18:28:24 +0200
changeset 71 67e3234c26a9
parent 70 155fdea254c3
child 72 8c7a2119dc9d
Add --stdin to send a message from standard input
cmd/status.go
cmd/toot.go
--- a/cmd/status.go	Sat May 06 18:16:08 2017 +0200
+++ b/cmd/status.go	Sat May 06 18:28:24 2017 +0200
@@ -28,6 +28,7 @@
 	mediaIDs      string
 	mediaFilePath string
 	textFilePath  string
+	stdin         bool
 
 	// Used for several subcommands to limit the number of results
 	limit uint
@@ -60,6 +61,7 @@
 	statusPostSubcommand.Flags().StringVarP(&statusOpts.mediaFilePath, "file", "f", "", "Media file name")
 	statusPostSubcommand.Flags().StringVar(&statusOpts.textFilePath, "text-file", "", "Text file name (message content)")
 	statusPostSubcommand.Flags().Int64VarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to")
+	statusPostSubcommand.Flags().BoolVar(&statusOpts.stdin, "stdin", false, "Read message content from standard input")
 
 	// Flag completion
 	annotation := make(map[string][]string)
@@ -163,7 +165,8 @@
 	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 toot --text-file message.txt`,
+  madonctl status toot --text-file message.txt
+  echo "Hello from #madonctl" | madonctl status toot --stdin`,
 	RunE: func(cmd *cobra.Command, args []string) error {
 		return statusSubcommandRunE(cmd.Name(), args)
 	},
@@ -243,6 +246,12 @@
 				break
 			}
 			text = string(b)
+		} else if opt.stdin {
+			var b []byte
+			if b, err = ioutil.ReadAll(os.Stdin); err != nil {
+				break
+			}
+			text = string(b)
 		}
 		s, err = toot(text)
 		obj = s
--- a/cmd/toot.go	Sat May 06 18:16:08 2017 +0200
+++ b/cmd/toot.go	Sat May 06 18:28:24 2017 +0200
@@ -24,6 +24,7 @@
 	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().Int64VarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to")
+	tootAliasCmd.Flags().BoolVar(&statusOpts.stdin, "stdin", false, "Read message content from standard input")
 
 	// Flag completion
 	annotation := make(map[string][]string)
@@ -40,7 +41,8 @@
   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 toot --text-file message.txt`,
+  madonctl toot --text-file message.txt
+  echo "Hello from #madonctl" | madonctl toot --visibility unlisted --stdin`,
 	RunE: func(cmd *cobra.Command, args []string) error {
 		if err := madonInit(true); err != nil {
 			return err