small refactoring

pull/5/head
Yasuhiro Matsumoto 2017-04-14 12:30:13 +09:00
parent 0281f9c746
commit e8fd17094f
1 changed files with 34 additions and 35 deletions

View File

@ -34,7 +34,15 @@ func readFile(filename string) ([]byte, error) {
return ioutil.ReadFile(filename)
}
func extractText(node *html.Node, w *bytes.Buffer) {
func textContent(s string) string {
doc, err := html.Parse(strings.NewReader(s))
if err != nil {
log.Fatal(err)
}
var buf bytes.Buffer
var extractText func(node *html.Node, w *bytes.Buffer)
extractText = func(node *html.Node, w *bytes.Buffer) {
if node.Type == html.TextNode {
data := strings.Trim(node.Data, "\r\n")
if data != "" {
@ -44,14 +52,7 @@ func extractText(node *html.Node, w *bytes.Buffer) {
for c := node.FirstChild; c != nil; c = c.NextSibling {
extractText(c, w)
}
}
func textContent(s string) string {
doc, err := html.Parse(strings.NewReader(s))
if err != nil {
log.Fatal(err)
}
var buf bytes.Buffer
extractText(doc, &buf)
return buf.String()
}
@ -111,17 +112,7 @@ func getConfig() (string, *mastodon.Config, error) {
return file, config, nil
}
func main() {
flag.Parse()
file, config, err := getConfig()
if err != nil {
log.Fatal(err)
}
client := mastodon.NewClient(config)
if config.AccessToken == "" {
func authenticate(client *mastodon.Client, config *mastodon.Config, file string) {
email, password, err := prompt()
if err != nil {
log.Fatal(err)
@ -138,6 +129,20 @@ func main() {
if err != nil {
log.Fatal("failed to store file:", err)
}
}
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 != "" {
@ -145,13 +150,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
_, err = client.PostStatus(&mastodon.Toot{
Status: string(text),
})
if err != nil {
log.Fatal(err)
}
return
*toot = string(text)
}
if *toot != "" {