parent
cc5f65b3bf
commit
bade3895da
|
@ -7,6 +7,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
"github.com/mattn/go-mastodon"
|
"github.com/mattn/go-mastodon"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
|
@ -20,9 +21,35 @@ type SimpleJSON struct {
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkFlag(f ...bool) bool {
|
||||||
|
n := 0
|
||||||
|
for _, on := range f {
|
||||||
|
if on {
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n > 1
|
||||||
|
}
|
||||||
|
|
||||||
func cmdStream(c *cli.Context) error {
|
func cmdStream(c *cli.Context) error {
|
||||||
asJSON := c.Bool("json")
|
asJSON := c.Bool("json")
|
||||||
asSimpleJSON := c.Bool("simplejson")
|
asSimpleJSON := c.Bool("simplejson")
|
||||||
|
asFormat := c.String("template")
|
||||||
|
|
||||||
|
if checkFlag(asJSON, asSimpleJSON, asFormat != "") {
|
||||||
|
return errors.New("cannot speicify two or three options in --json/--simplejson/--template")
|
||||||
|
}
|
||||||
|
tx, err := template.New("mstdn").Funcs(template.FuncMap{
|
||||||
|
"nl": func(s string) string {
|
||||||
|
return s + "\n"
|
||||||
|
},
|
||||||
|
"text": func(s string) string {
|
||||||
|
return textContent(s)
|
||||||
|
},
|
||||||
|
}).Parse(asFormat)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
client := c.App.Metadata["client"].(*mastodon.Client)
|
client := c.App.Metadata["client"].(*mastodon.Client)
|
||||||
config := c.App.Metadata["config"].(*mastodon.Config)
|
config := c.App.Metadata["config"].(*mastodon.Config)
|
||||||
|
@ -33,7 +60,6 @@ func cmdStream(c *cli.Context) error {
|
||||||
signal.Notify(sc, os.Interrupt)
|
signal.Notify(sc, os.Interrupt)
|
||||||
|
|
||||||
var q chan mastodon.Event
|
var q chan mastodon.Event
|
||||||
var err error
|
|
||||||
|
|
||||||
t := c.String("type")
|
t := c.String("type")
|
||||||
if t == "public" {
|
if t == "public" {
|
||||||
|
@ -72,6 +98,8 @@ func cmdStream(c *cli.Context) error {
|
||||||
Content: textContent(t.Status.Content),
|
Content: textContent(t.Status.Content),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
} else if asFormat != "" {
|
||||||
|
tx.ExecuteTemplate(c.App.Writer, "mstdn", e)
|
||||||
} else {
|
} else {
|
||||||
switch t := e.(type) {
|
switch t := e.(type) {
|
||||||
case *mastodon.UpdateEvent:
|
case *mastodon.UpdateEvent:
|
||||||
|
|
|
@ -202,18 +202,22 @@ func makeApp() *cli.App {
|
||||||
Name: "stream",
|
Name: "stream",
|
||||||
Usage: "stream statuses",
|
Usage: "stream statuses",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "type",
|
||||||
|
Usage: "stream type (public,public/local,user:NAME,hashtag:TAG)",
|
||||||
|
},
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "json",
|
Name: "json",
|
||||||
Usage: "output JSON",
|
Usage: "output JSON",
|
||||||
},
|
},
|
||||||
cli.StringFlag{
|
|
||||||
Name: "type",
|
|
||||||
Usage: "straem type (public,public/local,user:NAME,hashtag:TAG)",
|
|
||||||
},
|
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "simplejson",
|
Name: "simplejson",
|
||||||
Usage: "output simple JSON",
|
Usage: "output simple JSON",
|
||||||
},
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "template",
|
||||||
|
Usage: "output with tamplate format",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Action: cmdStream,
|
Action: cmdStream,
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue
Block a user