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

View File

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