From b87bcf7ec70eb4b6dff84481968582bb9fb30156 Mon Sep 17 00:00:00 2001 From: sfomuseumbot Date: Fri, 3 Mar 2023 18:15:36 -0800 Subject: [PATCH] add ?newline= parameter for stdout publisher constructor --- publisher/gocloud.go | 2 +- publisher/publisher.go | 2 +- publisher/redis.go | 2 +- publisher/stdout.go | 27 +++++++++++++++++++++++++++ publisher/stdout_test.go | 26 ++++++++++++++++++++++++++ subscriber/gocloud.go | 6 +++--- 6 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 publisher/stdout_test.go diff --git a/publisher/gocloud.go b/publisher/gocloud.go index 5c82752..77dfdb3 100644 --- a/publisher/gocloud.go +++ b/publisher/gocloud.go @@ -2,7 +2,7 @@ package publisher import ( "context" - + "gocloud.dev/pubsub" ) diff --git a/publisher/publisher.go b/publisher/publisher.go index 649a1cd..664ea41 100644 --- a/publisher/publisher.go +++ b/publisher/publisher.go @@ -8,7 +8,7 @@ import ( "sort" "strings" - "github.com/aaronland/go-roster" + "github.com/aaronland/go-roster" ) type Publisher interface { diff --git a/publisher/redis.go b/publisher/redis.go index 45275a6..7b39391 100644 --- a/publisher/redis.go +++ b/publisher/redis.go @@ -5,7 +5,7 @@ import ( "fmt" "net/url" - "github.com/go-redis/redis/v8" + "github.com/go-redis/redis/v8" ) type RedisPublisher struct { diff --git a/publisher/stdout.go b/publisher/stdout.go index 51c3b07..1f41be7 100644 --- a/publisher/stdout.go +++ b/publisher/stdout.go @@ -2,11 +2,15 @@ package publisher import ( "context" + "fmt" + "net/url" "os" + "strconv" ) type StdoutPublisher struct { Publisher + apply_newline bool } func init() { @@ -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 } diff --git a/publisher/stdout_test.go b/publisher/stdout_test.go new file mode 100644 index 0000000..c857f22 --- /dev/null +++ b/publisher/stdout_test.go @@ -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) + } + } +} diff --git a/subscriber/gocloud.go b/subscriber/gocloud.go index bfceec0..d257d5b 100644 --- a/subscriber/gocloud.go +++ b/subscriber/gocloud.go @@ -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)