~gjabell/mfn

ref: e3e621af42a3deb6ad50e41320204010d5a389f6 mfn/config.go -rw-r--r-- 2.9 KiB View raw
e3e621afGalen Abell Ensure at least one notifier and add list command 3 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
package main

import (
	"fmt"
	"html/template"

	"github.com/BurntSushi/toml"
)

type EmailConfig struct {
	SubjectTemplate    *template.Template `toml:"-"`
	SubjectTemplateStr string             `toml:"subject_template"`
	BodyTemplate       *template.Template `toml:"-"`
	BodyTemplateStr    string             `toml:"body_template"`
	EmailTo            string             `toml:"to"`
	EmailFrom          string             `toml:"from"`
	Username           string             `toml:"username"`
	Password           string             `toml:"password"`
	Server             string             `toml:"server"`
}

func (c *EmailConfig) validate() error {
	for k, v := range map[string]string{
		"subject template": c.SubjectTemplateStr,
		"body template":    c.BodyTemplateStr,
		"\"to\" email":     c.EmailTo,
		"\"from\" email":   c.EmailFrom,
		"username":         c.Username,
		"password":         c.Password,
		"server":           c.Server,
	} {
		if v == "" {
			return fmt.Errorf("you must provide a valid %s", k)
		}
	}

	// parse the templates
	subjTmpl, err := template.New("subject_template").Parse(c.SubjectTemplateStr)
	if err != nil {
		return err
	}
	c.SubjectTemplate = subjTmpl

	bodyTmpl, err := template.New("body_template").Parse(c.BodyTemplateStr)
	if err != nil {
		return err
	}
	c.BodyTemplate = bodyTmpl

	return nil
}

type WebhookConfig struct {
	Template    *template.Template `toml:"-"`
	TemplateStr string             `toml:"template"`
	Endpoint    string             `toml:"endpoint"`
}

func (c *WebhookConfig) validate() error {
	for k, v := range map[string]string{
		"template": c.TemplateStr,
		"endpoint": c.Endpoint,
	} {
		if v == "" {
			return fmt.Errorf("you must provide a valid %s", k)
		}
	}

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

	return nil
}

type Config struct {
	Server   string `toml:"server"`
	Username string `toml:"username"`
	Password string `toml:"password"`
	DbPath   string `toml:"db_path"`

	EmailConfig   *EmailConfig   `toml:"email"`
	WebhookConfig *WebhookConfig `toml:"webhook"`
}

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,
	} {
		if v == "" {
			return fmt.Errorf("you must provide a valid %s", k)
		}
	}

	if c.EmailConfig != nil {
		if err := c.EmailConfig.validate(); err != nil {
			return fmt.Errorf("invalid email config: %s", err)
		}
	}

	if c.WebhookConfig != nil {
		if err := c.WebhookConfig.validate(); err != nil {
			return fmt.Errorf("invalid webhook config: %s", err)
		}
	}

	return nil
}