~gjabell/mfn

fd0ff9537d1966e674a35e898687396355ca8be1 — Galen Abell 1 year, 2 months ago c133234
Rename to mfn and refactor config
4 files changed, 72 insertions(+), 48 deletions(-)

M .gitignore
A config.go
M go.mod
M main.go
M .gitignore => .gitignore +1 -1
@@ 1,3 1,3 @@
mfw
mfn
config.toml
last_entry.txt
\ No newline at end of file

A config.go => config.go +56 -0
@@ 0,0 1,56 @@
package main

import (
	"fmt"
	"html/template"

	"github.com/BurntSushi/toml"
)

type config struct {
	Server             string             `toml:"server"`
	Username           string             `toml:"username"`
	Password           string             `toml:"password"`
	DbPath             string             `toml:"db_path"`
	WebhookTemplateStr string             `toml:"webhook_template"`
	WebhookTemplate    *template.Template `toml:"-"`
	WebhookURL         string             `toml:"webhook_url"`
}

func loadConfig(configPath string) (*config, error) {
	var c config

	if _, err := toml.DecodeFile(configPath, &c); err != nil {
		return nil, err
	}

	if err := c.validate(); err != nil {
		return nil, err
	}

	return &c, nil
}

func (c *config) validate() error {
	for k, v := range map[string]string{
		"server url":       c.Server,
		"username":         c.Username,
		"password":         c.Password,
		"database path":    c.DbPath,
		"webhook template": c.WebhookTemplateStr,
		"webhook url":      c.WebhookURL,
	} {
		if v == "" {
			return fmt.Errorf("you must provide a valid %s", k)
		}
	}

	// parse the template
	tmpl, err := template.New("webhook").Parse(c.WebhookTemplateStr)
	if err != nil {
		return err
	}
	c.WebhookTemplate = tmpl

	return nil
}

M go.mod => go.mod +1 -1
@@ 1,4 1,4 @@
module git.sr.ht/~gjabell/mfw
module git.sr.ht/~gjabell/mfn

go 1.13


M main.go => main.go +14 -46
@@ 14,7 14,6 @@ import (
	"time"

	"git.sr.ht/~sircmpwn/getopt"
	"github.com/BurntSushi/toml"

	mf "miniflux.app/client"
)


@@ 22,46 21,6 @@ import (
var version = "0.1.0"
var webhookTemplate *template.Template

type config struct {
	Server          string `toml:"server"`
	Username        string `toml:"username"`
	Password        string `toml:"password"`
	DbPath          string `toml:"db_path"`
	WebhookTemplate string `toml:"webhook_template"`
	WebhookURL      string `toml:"webhook_url"`
}

func loadConfig(configPath string) (*config, error) {
	var c config

	if _, err := toml.DecodeFile(configPath, &c); err != nil {
		return nil, err
	}

	// validation
	for k, v := range map[string]string{
		"server url":       c.Server,
		"username":         c.Username,
		"password":         c.Password,
		"database path":    c.DbPath,
		"webhook template": c.WebhookTemplate,
		"webhook url":      c.WebhookURL,
	} {
		if v == "" {
			return nil, fmt.Errorf("you must provide a valid %s", k)
		}
	}

	// parse the template
	tmpl, err := template.New("webhook").Parse(c.WebhookTemplate)
	if err != nil {
		return nil, err
	}
	webhookTemplate = tmpl

	return &c, nil
}

func loadLastEntry(filepath string) (int64, error) {
	bytes, err := ioutil.ReadFile(filepath)
	if err != nil {


@@ 143,12 102,12 @@ func main() {
	}

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

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



@@ 160,6 119,7 @@ func main() {
	if err != nil {
		log.Fatalf("Failed to load config: %s\n", err)
	}
	webhookTemplate = conf.WebhookTemplate

	if *webhookTest {
		log.Println("Sending test webhook...")


@@ 169,11 129,17 @@ func main() {
		os.Exit(0)
	}

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

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

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

	var notify bool


@@ 197,7 163,7 @@ func main() {

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

	newEntries := newEntriesSet.Entries


@@ 218,6 184,8 @@ func main() {
	}
	log.Printf("Saving new last entry: %d\n", lastEntry)
	if err := saveLastEntry(conf.DbPath, lastEntry); err != nil {
		log.Fatalf("Failed to save new entries: %s\n", err)
		return fmt.Errorf("failed to save new entries: %s", err)
	}

	return nil
}