small refactoring

pull/5/head
Yasuhiro Matsumoto 2017-04-14 12:33:12 +09:00
parent e8fd17094f
commit 15f78b2da3
1 changed files with 51 additions and 40 deletions

View File

@ -131,9 +131,55 @@ func authenticate(client *mastodon.Client, config *mastodon.Config, file string)
} }
} }
func main() { func streaming(client *mastodon.Client) {
flag.Parse() 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 {
log.Fatal(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)
}
}
}
func init() {
flag.Parse()
if *fromfile != "" {
text, err := readFile(*fromfile)
if err != nil {
log.Fatal(err)
}
*toot = string(text)
}
}
func post(client *mastodon.Client, text string) {
_, err := client.PostStatus(&mastodon.Toot{
Status: text,
})
if err != nil {
log.Fatal(err)
}
}
func main() {
file, config, err := getConfig() file, config, err := getConfig()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -145,49 +191,14 @@ func main() {
authenticate(client, config, file) authenticate(client, config, file)
return return
} }
if *fromfile != "" {
text, err := readFile(*fromfile)
if err != nil {
log.Fatal(err)
}
*toot = string(text)
}
if *toot != "" { if *toot != "" {
_, err = client.PostStatus(&mastodon.Toot{ post(client, *toot)
Status: *toot,
})
if err != nil {
log.Fatal(err)
}
return return
} }
if *stream { if *stream {
ctx, cancel := context.WithCancel(context.Background()) streaming(client)
sc := make(chan os.Signal, 1)
signal.Notify(sc, os.Interrupt)
q, err := client.StreamingPublic(ctx)
if err != nil {
log.Fatal(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 return
} }