separate command

pull/13/head
Yasuhiro Matsumoto 2017-04-15 21:52:11 +09:00
parent 094a888b1d
commit 209a15a4d7
4 changed files with 100 additions and 76 deletions

View File

@ -0,0 +1,42 @@
package main
import (
"context"
"fmt"
"os"
"os/signal"
"github.com/fatih/color"
"github.com/mattn/go-mastodon"
"github.com/urfave/cli"
)
func cmdStream(c *cli.Context) error {
client := c.App.Metadata["client"].(*mastodon.Client)
ctx, cancel := context.WithCancel(context.Background())
sc := make(chan os.Signal, 1)
signal.Notify(sc, os.Interrupt)
q, err := client.StreamingPublic(ctx)
if err != nil {
return err
}
go func() {
<-sc
cancel()
close(q)
}()
for e := range q {
switch t := e.(type) {
case *mastodon.UpdateEvent:
color.Set(color.FgHiRed)
fmt.Println(t.Status.Account.Username)
color.Set(color.Reset)
fmt.Println(textContent(t.Status.Content))
case *mastodon.ErrorEvent:
color.Set(color.FgYellow)
fmt.Println(t.Error())
color.Set(color.Reset)
}
}
return nil
}

View File

@ -0,0 +1,25 @@
package main
import (
"fmt"
"github.com/fatih/color"
"github.com/mattn/go-mastodon"
"github.com/urfave/cli"
)
func cmdTimeline(c *cli.Context) error {
client := c.App.Metadata["client"].(*mastodon.Client)
timeline, err := client.GetTimelineHome()
if err != nil {
return err
}
for i := len(timeline) - 1; i >= 0; i-- {
t := timeline[i]
color.Set(color.FgHiRed)
fmt.Println(t.Account.Username)
color.Set(color.Reset)
fmt.Println(textContent(t.Content))
}
return nil
}

View File

@ -0,0 +1,33 @@
package main
import (
"errors"
"log"
"strings"
"github.com/mattn/go-mastodon"
"github.com/urfave/cli"
)
func cmdToot(c *cli.Context) error {
if !c.Args().Present() {
return errors.New("arguments required")
}
var toot string
ff := c.String("ff")
if ff != "" {
text, err := readFile(ff)
if err != nil {
log.Fatal(err)
}
toot = string(text)
} else {
toot = strings.Join(c.Args().Tail(), " ")
}
client := c.App.Metadata["client"].(*mastodon.Client)
_, err := client.PostStatus(&mastodon.Toot{
Status: toot,
})
return err
}

View File

@ -3,27 +3,20 @@ package main
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"github.com/fatih/color"
"github.com/mattn/go-mastodon"
"github.com/mattn/go-tty"
"github.com/urfave/cli"
"golang.org/x/net/html"
)
var ()
func readFile(filename string) ([]byte, error) {
if filename == "-" {
return ioutil.ReadAll(os.Stdin)
@ -204,75 +197,6 @@ func run() int {
return 0
}
func cmdToot(c *cli.Context) error {
if !c.Args().Present() {
return errors.New("arguments required")
}
var toot string
ff := c.String("ff")
if ff != "" {
text, err := readFile(ff)
if err != nil {
log.Fatal(err)
}
toot = string(text)
} else {
toot = strings.Join(c.Args().Tail(), " ")
}
client := c.App.Metadata["client"].(*mastodon.Client)
_, err := client.PostStatus(&mastodon.Toot{
Status: toot,
})
return err
}
func cmdStream(c *cli.Context) error {
client := c.App.Metadata["client"].(*mastodon.Client)
ctx, cancel := context.WithCancel(context.Background())
sc := make(chan os.Signal, 1)
signal.Notify(sc, os.Interrupt)
q, err := client.StreamingPublic(ctx)
if err != nil {
return err
}
go func() {
<-sc
cancel()
close(q)
}()
for e := range q {
switch t := e.(type) {
case *mastodon.UpdateEvent:
color.Set(color.FgHiRed)
fmt.Println(t.Status.Account.Username)
color.Set(color.Reset)
fmt.Println(textContent(t.Status.Content))
case *mastodon.ErrorEvent:
color.Set(color.FgYellow)
fmt.Println(t.Error())
color.Set(color.Reset)
}
}
return nil
}
func cmdTimeline(c *cli.Context) error {
client := c.App.Metadata["client"].(*mastodon.Client)
timeline, err := client.GetTimelineHome()
if err != nil {
return err
}
for i := len(timeline) - 1; i >= 0; i-- {
t := timeline[i]
color.Set(color.FgHiRed)
fmt.Println(t.Account.Username)
color.Set(color.Reset)
fmt.Println(textContent(t.Content))
}
return nil
}
func main() {
os.Exit(run())
}