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