aboutsummaryrefslogtreecommitdiffstats
path: root/exporter/exporter.go
blob: 8e7ff12c31115693de0e614eb5724245dc2b0c5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package exporter

import (
	"adammathes.com/neko/config"
	"adammathes.com/neko/models/feed"
	"encoding/json"
	"encoding/xml"
	"fmt"
	"html/template"
	"os"
	"path"
)

func ExportFeeds(format string) {
	feeds, err := feed.All()
	if err != nil {
		panic(err)
	}

	switch format {
	case "text":
		for _, f := range feeds {
			fmt.Printf("%s\n", f.Url)
		}

	case "opml":
		fmt.Printf(`<opml version="2.0"><head><title>neko feeds</title></head><body>`)
		fmt.Printf("\n")
		for _, f := range feeds {
			b, _ := xml.Marshal(f)
			fmt.Printf("%s\n", string(b))
		}
		fmt.Printf(`</body></opml>`)

	case "json":
		js, _ := json.Marshal(feeds)
		fmt.Printf("%s\n", js)

	case "html":
		tmplFile := path.Join(config.Config.StaticDir, "feeds.tmpl")
		feedsTmpl := template.Must(template.ParseFiles(tmplFile))
		err := feedsTmpl.Execute(os.Stdout, feeds)
		if err != nil {
			panic(err)
		}
	}
}