add stream -simplejson

This commit is contained in:
Yasuhiro Matsumoto 2017-04-17 23:43:11 +09:00
parent 0579b31633
commit eb0803d442
2 changed files with 19 additions and 0 deletions

View File

@ -12,8 +12,15 @@ import (
"github.com/urfave/cli" "github.com/urfave/cli"
) )
type SimpleJSON struct {
Username string `json:"username"`
Avatar string `json:"avatar"`
Content string `json:"content"`
}
func cmdStream(c *cli.Context) error { func cmdStream(c *cli.Context) error {
asJSON := c.Bool("json") asJSON := c.Bool("json")
asSimpleJSON := c.Bool("simplejson")
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()
@ -31,6 +38,14 @@ func cmdStream(c *cli.Context) error {
for e := range q { for e := range q {
if asJSON { if asJSON {
json.NewEncoder(c.App.Writer).Encode(e) json.NewEncoder(c.App.Writer).Encode(e)
} else if asSimpleJSON {
if t, ok := e.(*mastodon.UpdateEvent); ok {
json.NewEncoder(c.App.Writer).Encode(&SimpleJSON{
Username: t.Status.Account.Username,
Avatar: t.Status.Account.AvatarStatic,
Content: textContent(t.Status.Content),
})
}
} else { } else {
switch t := e.(type) { switch t := e.(type) {
case *mastodon.UpdateEvent: case *mastodon.UpdateEvent:

View File

@ -185,6 +185,10 @@ func makeApp() *cli.App {
Name: "json", Name: "json",
Usage: "output JSON", Usage: "output JSON",
}, },
cli.BoolFlag{
Name: "simplejson",
Usage: "output simple JSON",
},
}, },
Action: cmdStream, Action: cmdStream,
}, },