diff --git a/cmd/message.go b/cmd/message.go index 2c3fc36..87b76ad 100644 --- a/cmd/message.go +++ b/cmd/message.go @@ -321,10 +321,11 @@ var messageReactRemoveCmd = &cobra.Command{ } // resolveMessageText resolves a message body from, in order of precedence: -// the --file flag ('-' means stdin), the positional text argument at textArg, -// or piped stdin when neither is given. This lets callers avoid passing -// formatted message bodies as shell arguments, where backticks, $, and quotes -// would otherwise be interpreted by the shell and corrupt the message. +// the --file flag ('-' means stdin), the positional text argument at textArg +// ('-' also means stdin there), or piped stdin when neither is given. This +// lets callers avoid passing formatted message bodies as shell arguments, +// where backticks, $, and quotes would otherwise be interpreted by the shell +// and corrupt the message. func resolveMessageText(cmd *cobra.Command, args []string, textArg int) (string, error) { file, _ := cmd.Flags().GetString("file") @@ -337,6 +338,8 @@ func resolveMessageText(cmd *cobra.Command, args []string, textArg int) (string, return "", fmt.Errorf("reading message body from %s: %w", file, err) } return string(b), nil + case len(args) > textArg && args[textArg] == "-": + return readAllStdin() case len(args) > textArg: return args[textArg], nil case stdinIsPiped(): diff --git a/cmd/message_test.go b/cmd/message_test.go new file mode 100644 index 0000000..07b71b4 --- /dev/null +++ b/cmd/message_test.go @@ -0,0 +1,30 @@ +package cmd + +import ( + "os" + "strings" + "testing" +) + +func TestResolveMessageTextDashStdin(t *testing.T) { + r, w, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + orig := os.Stdin + os.Stdin = r + defer func() { os.Stdin = orig }() + + go func() { + w.WriteString("hello from stdin\n") + w.Close() + }() + + got, err := resolveMessageText(messageSendCmd, []string{"D0AHB4FLY75", "-"}, 1) + if err != nil { + t.Fatal(err) + } + if strings.TrimSpace(got) != "hello from stdin" { + t.Fatalf("got %q, want stdin content", got) + } +}