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,16 +29,20 @@ func cmdStream(c *cli.Context) error {
close(q)
}()
for e := range q {
switch t := e.(type) {
case *mastodon.UpdateEvent:
color.Set(color.FgHiRed)
fmt.Fprintln(c.App.Writer, t.Status.Account.Username)
color.Set(color.Reset)
fmt.Fprintln(c.App.Writer, textContent(t.Status.Content))
case *mastodon.ErrorEvent:
color.Set(color.FgYellow)
fmt.Fprintln(c.App.Writer, t.Error())
color.Set(color.Reset)
if asJSON {
json.NewEncoder(c.App.Writer).Encode(e)
} else {
switch t := e.(type) {
case *mastodon.UpdateEvent:
color.Set(color.FgHiRed)
fmt.Fprintln(c.App.Writer, t.Status.Account.Username)
color.Set(color.Reset)
fmt.Fprintln(c.App.Writer, textContent(t.Status.Content))
case *mastodon.ErrorEvent:
color.Set(color.FgYellow)
fmt.Fprintln(c.App.Writer, t.Error())
color.Set(color.Reset)
}
}
}
return nil

View File

@ -178,8 +178,14 @@ func makeApp() *cli.App {
Action: cmdToot,
},
{
Name: "stream",
Usage: "stream statuses",
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() {}