aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
blob: ade519758761c0ba4a80b09d7cda7be8fd35fd96 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main

import (
	"fmt"
	"os"
	"time"

	"adammathes.com/neko/config"
	"adammathes.com/neko/crawler"
	"adammathes.com/neko/exporter"
	"adammathes.com/neko/models"
	"adammathes.com/neko/models/feed"
	"adammathes.com/neko/tui"
	"adammathes.com/neko/vlog"
	"adammathes.com/neko/web"
	flag "github.com/ogier/pflag"
)

var Version, Build string

func main() {
	if err := Run(os.Args[1:]); err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		os.Exit(1)
	}
}

func Run(args []string) error {
	var help, update, verbose, proxyImages, tuiMode bool
	var configFile, dbfile, newFeed, export, password string
	var port, minutes int

	f := flag.NewFlagSet("neko", flag.ContinueOnError)

	// config file
	f.StringVarP(&configFile, "config", "c", "", "read configuration from file")

	// commands -- no command runs the web server
	f.BoolVarP(&help, "help", "h", false, "print usage information")
	f.BoolVarP(&update, "update", "u", false, "fetch feeds and store new items")
	f.StringVarP(&newFeed, "add", "a", "", "add the feed at URL `http://example.com/rss.xml`")
	f.StringVarP(&export, "export", "x", "", "export feed. format required: text, opml, html, or json")

	// options -- defaults are set in config/main.go and overridden by cmd line
	f.StringVarP(&dbfile, "database", "d", "", "sqlite database file")
	f.IntVarP(&port, "http", "s", 0, "HTTP port to serve on")
	f.IntVarP(&minutes, "minutes", "m", 0, "minutes between crawling feeds")
	f.BoolVarP(&proxyImages, "imageproxy", "i", false, "rewrite and proxy all image requests for privacy (experimental)")
	f.BoolVarP(&tuiMode, "tui", "t", false, "launch terminal UI")
	f.BoolVarP(&verbose, "verbose", "v", false, "verbose output")

	// passwords on command line are bad, you should use the config file
	f.StringVarP(&password, "password", "p", "", "password to access web interface")

	f.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		f.PrintDefaults()
	}

	if err := f.Parse(args); err != nil {
		return err
	}

	if help {
		fmt.Printf("neko v%s | build %s\n", Version, Build)
		f.Usage()
		return nil
	}
	// reads config if present and sets defaults
	if err := config.Init(configFile); err != nil {
		return fmt.Errorf("config error: %v", err)
	}

	// override config file with flags if present
	vlog.VERBOSE = verbose
	if dbfile != "" {
		config.Config.DBFile = dbfile
	}

	if port != 0 {
		config.Config.Port = port
	}

	if password != "" {
		config.Config.DigestPassword = password
	}

	if minutes != 0 {
		config.Config.CrawlMinutes = minutes
	}

	if proxyImages != false {
		config.Config.ProxyImages = proxyImages
	}

	models.InitDB()

	if update {
		vlog.Printf("starting crawl\n")
		crawler.Crawl()
		return nil
	}
	if newFeed != "" {
		vlog.Printf("creating new feed\n")
		feed.NewFeed(newFeed)
		return nil
	}
	if export != "" {
		vlog.Printf("exporting feeds in format %s\n", export)
		fmt.Printf("%s", exporter.ExportFeeds(export))
		return nil
	}

	if tuiMode {
		return tui.Run()
	}

	// For testing, we might want to avoid starting a web server
	if config.Config.Port == -1 {
		return nil
	}

	go backgroundCrawl(config.Config.CrawlMinutes)
	vlog.Printf("starting web server at 127.0.0.1:%d\n",
		config.Config.Port)
	web.Serve()
	return nil
}

func backgroundCrawl(minutes int) {
	if minutes < 1 {
		return
	}
	vlog.Printf("starting background crawl every %d minutes\n", minutes)
	for {
		time.Sleep(time.Minute * time.Duration(minutes))
		crawler.Crawl()
	}
}