Revert "Fix Client API"

This reverts commit 5fe134909b.
This commit is contained in:
James Mills 2022-03-28 01:35:53 +10:00
parent 5fe134909b
commit 5c19049da3
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
2 changed files with 17 additions and 9 deletions

View File

@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
@ -73,6 +74,18 @@ func NewClient(url string, options *Options) *Client {
return client
}
// Handle ...
func (c *Client) Handle(msg *msgbus.Message) error {
out, err := json.Marshal(msg)
if err != nil {
return err
}
os.Stdout.Write(out)
os.Stdout.Write([]byte{'\r', '\n'})
return nil
}
// Pull ...
func (c *Client) Pull(topic string) (msg *msgbus.Message, err error) {
c.RLock()
@ -100,6 +113,9 @@ func (c *Client) Pull(topic string) (msg *msgbus.Message, err error) {
if err := json.NewDecoder(res.Body).Decode(&msg); err != nil {
return nil, err
}
if err := c.Handle(msg); err != nil {
return nil, err
}
return msg, nil
}

View File

@ -1,9 +1,6 @@
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -41,10 +38,5 @@ func pull(client *client.Client, topic string) {
topic = defaultTopic
}
msg, err := client.Pull(topic)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading message: %s\n", err)
os.Exit(2)
}
fmt.Printf("%s\n", msg.Payload)
client.Pull(topic)
}