allow send to accept message from stdin

This commit is contained in:
finn 2022-03-19 01:38:25 -07:00
parent 4bc97fb829
commit 0f3e6dce72

View file

@ -18,6 +18,7 @@ package send
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -37,9 +38,10 @@ var (
toAddress *v1.JsonAddress
toGroup string
attachments []string
message string
SendMessageCmd = &cobra.Command{
Use: "send <group id | phone number> <message>",
Use: "send {group id | phone number} [message]",
Short: "send a message",
PreRun: func(cmd *cobra.Command, args []string) {
if account == "" {
@ -49,9 +51,20 @@ var (
common.Must(cmd.Help())
log.Fatal("No account specified. Please specify with --account or set a default")
}
if len(args) < 2 {
if len(args) < 1 {
common.Must(cmd.Help())
log.Fatal("must specify both destination (either group id or phone number) and message")
log.Fatal("must specify both destination (either group id or phone number)")
}
if len(args) == 1 {
messageBytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Println("error reading message from stdin, perhaps you meant to include it in the command line arguments?")
panic(err)
}
message = string(messageBytes)
} else {
message = strings.Join(args[1:], " ")
}
to, err := common.StringToAddress(args[0])
if err != nil {
@ -65,7 +78,7 @@ var (
req := v1.SendRequest{
Username: account,
MessageBody: strings.Join(args[1:], " "),
MessageBody: message,
Attachments: []*v1.JsonAttachment{},
RecipientAddress: toAddress,
RecipientGroupID: toGroup,