~gjabell/mfn

ref: f5eb5922459ba34300ccf5e8cce824ebe806d592 mfn/main.go -rw-r--r-- 3.5 KiB View raw
f5eb5922Galen Abell Allow missing config sections 2 months ago
                                                                                
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strconv"
	"strings"

	"git.sr.ht/~sircmpwn/getopt"

	mf "miniflux.app/client"
)

var version = "0.1.0"

type Mfn struct {
	conf      *Config
	notifiers map[string]Notifier
}

func loadLastEntry(filepath string) (int64, error) {
	bytes, err := ioutil.ReadFile(filepath)
	if err != nil {
		if os.IsNotExist(err) {
			// no last entry
			return -1, nil
		}
		return -1, err
	}

	str := strings.TrimSpace(string(bytes))
	id, err := strconv.ParseInt(str, 10, 64)
	if err != nil {
		return -1, err
	}

	return id, nil
}

func saveLastEntry(filepath string, id int64) error {
	return ioutil.WriteFile(filepath, []byte(strconv.FormatInt(id, 10)), 0644)
}

func main() {
	showHelp := getopt.Bool("h", false, "show usage")
	showVersion := getopt.Bool("v", false, "show version")

	var testNotifier string
	getopt.StringVar(&testNotifier, "t", "", "test a notifier")

	var configPath string
	getopt.StringVar(&configPath, "c", "", "configuration file path")

	err := getopt.Parse()
	if err != nil {
		log.Fatalf("Failed to parse args: %s", err)
	}

	if *showHelp {
		fmt.Println("Usage: mfn [-v] [-t endpoint] -c config")
		os.Exit(0)
	}

	if *showVersion {
		fmt.Printf("mfn %s\n", version)
		os.Exit(0)
	}

	if configPath == "" {
		log.Fatal("You must specify a config location")
	}

	conf, err := LoadConfig(configPath)
	if err != nil {
		log.Fatalf("Failed to load config: %s\n", err)
	}

	notifiers := make(map[string]Notifier)
	if conf.EmailConfig != nil {
		notifiers["email"] = NewEmail(conf)
	}
	if conf.WebhookConfig != nil {
		notifiers["webhook"] = NewWebhook(conf)
	}

	mfn := &Mfn{
		conf:      conf,
		notifiers: notifiers,
	}

	if testNotifier != "" {
		notifier, ok := mfn.notifiers[testNotifier]
		if !ok {
			log.Fatalf("Invalid notifier: %s\n", testNotifier)
		}

		log.Printf("Sending test %s notification...\n", testNotifier)
		if err := Test(notifier); err != nil {
			log.Fatalf("Test notification failed: %s\n", err)
		}
		log.Printf("Test %s notification sent!\n", testNotifier)
		os.Exit(0)
	}

	if err := mfn.execNotifications(); err != nil {
		log.Fatal(err)
	}
}

func (mfn *Mfn) execNotifications() error {
	client := mf.New(mfn.conf.Server, mfn.conf.Username, mfn.conf.Password)

	lastEntry, err := loadLastEntry(mfn.conf.DbPath)
	if err != nil {
		return fmt.Errorf("failed to load last entry: %s", err)
	}

	var notify bool
	filter := &mf.Filter{Order: "id"}
	if lastEntry == -1 {
		// this is the first run, since we don't have a last entry
		// in this case, just request the single most recent entry
		// this will be the entry with the largest ID
		filter.Limit = 1
		filter.Direction = "desc"
		// don't send notifications on the first run
		notify = false
	} else {
		// if we already have entries, request all entries since the
		// last entry ID
		filter.Limit = 0
		filter.Direction = "asc"
		filter.AfterEntryID = lastEntry
		notify = true
	}

	newEntriesSet, err := client.Entries(filter)
	if err != nil {
		return fmt.Errorf("failed to fetch entries: %s", err)
	}

	newEntries := newEntriesSet.Entries
	for _, e := range newEntries {
		log.Printf("New entry: %d\n", e.ID)

		if e.ID > lastEntry {
			lastEntry = e.ID
		}

		if !notify {
			continue
		}

		for name, notifier := range mfn.notifiers {
			if err := notifier.Notify(e); err != nil {
				log.Printf("Failed to send %s notification: %s\n", name, err)
			}
		}
	}
	log.Printf("Saving new last entry: %d\n", lastEntry)
	if err := saveLastEntry(mfn.conf.DbPath, lastEntry); err != nil {
		return fmt.Errorf("failed to save new entries: %s", err)
	}

	return nil
}