Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #105 from nats-io/multiline_stdin
Browse files Browse the repository at this point in the history
fix reading multi line inputs on stdin in nats pub
  • Loading branch information
ripienaar authored Feb 26, 2020
2 parents 527b44a + f12708b commit c3ed8be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
7 changes: 4 additions & 3 deletions nats/pub_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
package main

import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"

Expand Down Expand Up @@ -51,11 +51,12 @@ func (c *pubCmd) publish(pc *kingpin.ParseContext) error {
defer nc.Close()

if c.body == "" && terminal.IsTerminal(int(os.Stdout.Fd())) {
reader := bufio.NewReader(os.Stdin)
c.body, err = reader.ReadString('\n')
log.Println("Reading payload from STDIN")
body, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
c.body = string(body)
}

if c.body == "" {
Expand Down
12 changes: 11 additions & 1 deletion nats/sub_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package main

import (
"context"
"fmt"
"log"

"github.com/nats-io/nats.go"
Expand All @@ -24,13 +25,15 @@ import (
type subCmd struct {
subject string
queue string
raw bool
}

func configureSubCommand(app *kingpin.Application) {
c := &subCmd{}
act := app.Command("sub", "Generic subscription client").Action(c.subscribe)
act.Arg("subject", "Subject to subscribe to").Required().StringVar(&c.subject)
act.Flag("queue", "Subscribe to a named queue group").StringVar(&c.queue)
act.Flag("raw", "Show the raw data received").Short('r').BoolVar(&c.raw)
}

func (c *subCmd) subscribe(_ *kingpin.ParseContext) error {
Expand All @@ -44,6 +47,11 @@ func (c *subCmd) subscribe(_ *kingpin.ParseContext) error {

handler := func(m *nats.Msg) {
i += 1
if c.raw {
fmt.Println(string(m.Data))
return
}

log.Printf("[#%d] Received on [%s]: '%s'", i, m.Subject, string(m.Data))
}

Expand All @@ -59,7 +67,9 @@ func (c *subCmd) subscribe(_ *kingpin.ParseContext) error {
return err
}

log.Printf("Listening on [%s]", c.subject)
if !c.raw {
log.Printf("Listening on [%s]", c.subject)
}

<-context.Background().Done()

Expand Down

0 comments on commit c3ed8be

Please sign in to comment.