add stream -json

pull/24/head
Yasuhiro Matsumoto 2017-04-17 23:29:44 +09:00
parent b6d4c40dd6
commit e7f2469bc0
3 changed files with 27 additions and 13 deletions

View File

@ -2,6 +2,7 @@ package main
import (
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
@ -12,6 +13,7 @@ import (
)
func cmdStream(c *cli.Context) error {
asJSON := c.Bool("json")
client := c.App.Metadata["client"].(*mastodon.Client)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
@ -27,6 +29,9 @@ func cmdStream(c *cli.Context) error {
close(q)
}()
for e := range q {
if asJSON {
json.NewEncoder(c.App.Writer).Encode(e)
} else {
switch t := e.(type) {
case *mastodon.UpdateEvent:
color.Set(color.FgHiRed)
@ -39,5 +44,6 @@ func cmdStream(c *cli.Context) error {
color.Set(color.Reset)
}
}
}
return nil
}

View File

@ -180,6 +180,12 @@ func makeApp() *cli.App {
{
Name: "stream",
Usage: "stream statuses",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "json",
Usage: "output JSON",
},
},
Action: cmdStream,
},
{

View File

@ -14,7 +14,9 @@ import (
)
// UpdateEvent is struct for passing status event to app.
type UpdateEvent struct{ Status *Status }
type UpdateEvent struct {
Status *Status `json:"status"`
}
func (e *UpdateEvent) event() {}