small refactoring

This commit is contained in:
Yasuhiro Matsumoto 2017-04-14 12:33:12 +09:00
parent e8fd17094f
commit 15f78b2da3

View File

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