From a79904ece6ee6bf9a456f2a7df153292c07e211a Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Sun, 28 Sep 2014 18:17:02 +0400 Subject: [PATCH] CLI flush action --- cli/main.go | 13 +++++++++++++ client/client.go | 15 ++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/cli/main.go b/cli/main.go index 3a38255..48f8b7c 100644 --- a/cli/main.go +++ b/cli/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "io/ioutil" "os" @@ -89,6 +90,18 @@ func main() { } }, }, + { + Name: "flush", + Usage: "Flush all messages from given queues", + Action: func(c *cli.Context) { + if msgs := bsq.Flush(c.Args()...); msgs != nil { + jsn, _ := json.Marshal(msgs) + fmt.Println(string(jsn)) + } else { + fmt.Printf("Failed to flush queues %s\n", strings.Join(c.Args(), ", ")) + } + }, + }, { Name: "status", Usage: "Show server status", diff --git a/client/client.go b/client/client.go index 54796fa..6889299 100644 --- a/client/client.go +++ b/client/client.go @@ -103,32 +103,33 @@ func (c *Client) Subscribe(queues ...string) *Message { } } -func (c *Client) Flush(queues ...string) (messages []*Message) { +func (c *Client) Flush(queues ...string) []*Message { url := c.url(flushEndpoint, "?queues=", strings.Join(queues, ",")) _, body := c.get(url) var tmp []map[string]string if err := json.Unmarshal(body, &tmp); err != nil { - return + return nil } - messages = []*Message{} + messages := []*Message{} for _, msg := range tmp { messages = append(messages, &Message{msg["queue"], []byte(msg["message"])}) } - return + return messages } -func (c *Client) Status() (stat []*QueueInfo) { +func (c *Client) Status() []*QueueInfo { url := c.url(statusEndpoit) _, body := c.get(url) tmp := make(map[string]map[string]int) if err := json.Unmarshal(body, &tmp); err != nil { - return + return nil } + stat := []*QueueInfo{} for queue, info := range tmp { qi := &QueueInfo{ Name: queue, @@ -138,7 +139,7 @@ func (c *Client) Status() (stat []*QueueInfo) { stat = append(stat, qi) } - return + return stat } func (c *Client) Debug() *DebugInfo {