diff --git a/cmd/mstdn/cmd_stream.go b/cmd/mstdn/cmd_stream.go index 50aba7d..b7122f3 100644 --- a/cmd/mstdn/cmd_stream.go +++ b/cmd/mstdn/cmd_stream.go @@ -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 diff --git a/cmd/mstdn/main.go b/cmd/mstdn/main.go index cb11270..0e2cb4e 100644 --- a/cmd/mstdn/main.go +++ b/cmd/mstdn/main.go @@ -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, }, { diff --git a/streaming.go b/streaming.go index ded8aad..41ffe8d 100644 --- a/streaming.go +++ b/streaming.go @@ -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() {}