From 6fbcd85d17568fed75d847c563f75d04bdaf51ca Mon Sep 17 00:00:00 2001 From: srevn Date: Sun, 6 Jul 2025 10:23:32 +0300 Subject: [PATCH] Add piping support --- cmd/publish.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/cmd/publish.go b/cmd/publish.go index aaec35e9..89475fbd 100644 --- a/cmd/publish.go +++ b/cmd/publish.go @@ -254,6 +254,16 @@ func parseTopicMessageCommand(c *cli.Context) (topic string, message string, com if c.String("message") != "" { message = c.String("message") } + + // If no message provided and stdin has data, read from stdin + if message == "" && stdinHasData() { + var stdinBytes []byte + stdinBytes, err = io.ReadAll(c.App.Reader) + if err != nil { + return + } + message = strings.TrimSpace(string(stdinBytes)) + } return } @@ -312,3 +322,11 @@ func runAndWaitForCommand(command []string) (message string, err error) { log.Debug("Command succeeded after %s: %s", runtime, prettyCmd) return fmt.Sprintf("Command succeeded after %s: %s", runtime, prettyCmd), nil } + +func stdinHasData() bool { + stat, err := os.Stdin.Stat() + if err != nil { + return false + } + return (stat.Mode() & os.ModeCharDevice) == 0 +}