Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions cmd/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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():
Expand Down
30 changes: 30 additions & 0 deletions cmd/message_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading