mirror of
https://github.com/binwiederhier/ntfy.git
synced 2026-05-15 15:45:49 -06:00
Stream redirected stdin when publishing
This commit is contained in:
parent
6cfadf9681
commit
95ce413ccb
2 changed files with 43 additions and 12 deletions
|
|
@ -123,6 +123,8 @@ func execPublish(c *cli.Context) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Stream redirected stdin directly instead of buffering it into memory first.
|
||||
useStdinMessage := file == "" && message == "" && isStdinRedirected()
|
||||
var options []client.PublishOption
|
||||
if title != "" {
|
||||
options = append(options, client.WithTitle(title))
|
||||
|
|
@ -196,20 +198,24 @@ func execPublish(c *cli.Context) error {
|
|||
newMessage, err := waitForProcess(pid)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if message == "" {
|
||||
} else if message == "" && !useStdinMessage {
|
||||
message = newMessage
|
||||
}
|
||||
} else if len(command) > 0 {
|
||||
newMessage, err := runAndWaitForCommand(command)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if message == "" {
|
||||
} else if message == "" && !useStdinMessage {
|
||||
message = newMessage
|
||||
}
|
||||
}
|
||||
var body io.Reader
|
||||
if file == "" {
|
||||
body = strings.NewReader(message)
|
||||
if useStdinMessage {
|
||||
body = c.App.Reader
|
||||
} else {
|
||||
body = strings.NewReader(message)
|
||||
}
|
||||
} else {
|
||||
if message != "" {
|
||||
options = append(options, client.WithMessage(message))
|
||||
|
|
@ -266,15 +272,6 @@ func parseTopicMessageCommand(c *cli.Context) (topic string, message string, com
|
|||
if c.String("message") != "" {
|
||||
message = c.String("message")
|
||||
}
|
||||
if message == "" && isStdinRedirected() {
|
||||
var data []byte
|
||||
data, err = io.ReadAll(io.LimitReader(c.App.Reader, 1024*1024))
|
||||
if err != nil {
|
||||
log.Debug("Failed to read from stdin: %s", err.Error())
|
||||
return
|
||||
}
|
||||
message = strings.TrimSpace(string(data))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package cmd
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
|
|
@ -52,6 +53,39 @@ func TestCLI_Publish_Subscribe_Poll(t *testing.T) {
|
|||
require.Equal(t, "some message", m.Message)
|
||||
}
|
||||
|
||||
func TestCLI_Publish_Stdin_ReadsFullInput(t *testing.T) {
|
||||
input := strings.Repeat("x", 1024*1024+123)
|
||||
var received int
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
require.NoError(t, err)
|
||||
received = len(body)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, err = w.Write([]byte(`{"id":"abc","time":1,"event":"message","topic":"mytopic","message":"ok"}`))
|
||||
require.NoError(t, err)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
app, stdin, stdout, _ := newTestApp()
|
||||
_, err := stdin.WriteString(input)
|
||||
require.NoError(t, err)
|
||||
|
||||
f, err := os.CreateTemp(t.TempDir(), "stdin")
|
||||
require.NoError(t, err)
|
||||
oldStdin := os.Stdin
|
||||
os.Stdin = f
|
||||
t.Cleanup(func() {
|
||||
os.Stdin = oldStdin
|
||||
require.NoError(t, f.Close())
|
||||
})
|
||||
|
||||
require.NoError(t, app.Run([]string{"ntfy", "publish", server.URL + "/mytopic"}))
|
||||
require.Equal(t, len(input), received)
|
||||
require.Contains(t, stdout.String(), `"message":"ok"`)
|
||||
}
|
||||
|
||||
func TestCLI_Publish_All_The_Things(t *testing.T) {
|
||||
s, port := test.StartServer(t)
|
||||
defer test.StopServer(t, s, port)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue