Skip to content

Commit

Permalink
add ?newline= parameter for stdout publisher constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
sfomuseumbot committed Mar 4, 2023
1 parent c45cacb commit b87bcf7
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion publisher/gocloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package publisher

import (
"context"

"gocloud.dev/pubsub"
)

Expand Down
2 changes: 1 addition & 1 deletion publisher/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sort"
"strings"

"github.com/aaronland/go-roster"
"github.com/aaronland/go-roster"
)

type Publisher interface {
Expand Down
2 changes: 1 addition & 1 deletion publisher/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"net/url"

"github.com/go-redis/redis/v8"
"github.com/go-redis/redis/v8"
)

type RedisPublisher struct {
Expand Down
27 changes: 27 additions & 0 deletions publisher/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package publisher

import (
"context"
"fmt"
"net/url"
"os"
"strconv"
)

type StdoutPublisher struct {
Publisher
apply_newline bool
}

func init() {
Expand All @@ -16,13 +20,36 @@ func init() {

func NewStdoutPublisher(ctx context.Context, uri string) (Publisher, error) {

u, err := url.Parse(uri)

if err != nil {
return nil, fmt.Errorf("Failed to parse URI, %w", err)
}

q := u.Query()

pub := &StdoutPublisher{}

if q.Get("newline") != "" {

v, err := strconv.ParseBool(q.Get("newline"))

if err != nil {
return nil, fmt.Errorf("Failed to parse ?newline= parameter, %w", err)
}

pub.apply_newline = v
}

return pub, nil
}

func (pub *StdoutPublisher) Publish(ctx context.Context, msg string) error {

if pub.apply_newline {
msg = fmt.Sprintf("%s\n", msg)
}

os.Stdout.Write([]byte(msg))
return nil
}
Expand Down
26 changes: 26 additions & 0 deletions publisher/stdout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package publisher

import (
"context"
"testing"
)

func TestSdtoutPublisher(t *testing.T) {

uris := []string{
"stdout://",
"stdout://?newline=true",
"stdout://?newline=false",
}

ctx := context.Background()

for _, u := range uris {

_, err := NewPublisher(ctx, u)

if err != nil {
t.Fatalf("Failed to parse '%s', %v", u, err)
}
}
}
6 changes: 3 additions & 3 deletions subscriber/gocloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func NewGoCloudSubscriber(ctx context.Context, uri string) (Subscriber, error) {
func (sub *GoCloudSubscriber) Listen(ctx context.Context, msg_ch chan string) error {

for {

msg, err := sub.subscription.Receive(ctx)

if err != nil {
return err
}

go msg.Ack()

msg_ch <- string(msg.Body)
Expand Down

0 comments on commit b87bcf7

Please sign in to comment.