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

View File

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

View File

@ -14,7 +14,9 @@ import (
) )
// UpdateEvent is struct for passing status event to app. // 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() {} func (e *UpdateEvent) event() {}