2021-05-15 11:19:28 +02:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"io"
|
2022-09-14 16:14:18 +02:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2021-05-15 11:19:28 +02:00
|
|
|
"strings"
|
2021-06-25 09:14:27 +02:00
|
|
|
"time"
|
2021-05-15 11:19:28 +02:00
|
|
|
|
2024-09-17 07:33:26 +02:00
|
|
|
"go-mod.ewintr.nl/gte/internal/storage"
|
|
|
|
"go-mod.ewintr.nl/gte/pkg/msend"
|
|
|
|
"go-mod.ewintr.nl/gte/pkg/mstore"
|
2021-05-15 11:19:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrUnableToRead = errors.New("unable to read configuration")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Configuration struct {
|
2021-09-22 07:11:05 +02:00
|
|
|
IMAPURL string
|
|
|
|
IMAPUsername string
|
|
|
|
IMAPPassword string
|
|
|
|
IMAPFolderPrefix string
|
2021-05-15 11:19:28 +02:00
|
|
|
|
|
|
|
SMTPURL string
|
|
|
|
SMTPUsername string
|
|
|
|
SMTPPassword string
|
|
|
|
|
|
|
|
FromName string
|
|
|
|
FromAddress string
|
|
|
|
|
|
|
|
ToName string
|
|
|
|
ToAddress string
|
2021-06-25 09:14:27 +02:00
|
|
|
|
|
|
|
LocalDBPath string
|
2022-09-14 16:14:18 +02:00
|
|
|
DaysAhead int
|
2021-06-25 09:14:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type LocalConfiguration struct {
|
|
|
|
MinSyncInterval time.Duration
|
2021-05-15 11:19:28 +02:00
|
|
|
}
|
|
|
|
|
2022-09-14 16:14:18 +02:00
|
|
|
func NewFromFile(src io.Reader) *Configuration {
|
2021-05-15 11:19:28 +02:00
|
|
|
conf := &Configuration{}
|
|
|
|
scanner := bufio.NewScanner(src)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := strings.Split(scanner.Text(), "=")
|
|
|
|
if len(line) != 2 {
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
key, value := strings.TrimSpace(line[0]), strings.TrimSpace(line[1])
|
|
|
|
switch key {
|
|
|
|
case "imap_url":
|
|
|
|
conf.IMAPURL = value
|
|
|
|
case "imap_username":
|
|
|
|
conf.IMAPUsername = value
|
|
|
|
case "imap_password":
|
|
|
|
conf.IMAPPassword = value
|
2021-09-22 07:11:05 +02:00
|
|
|
case "imap_folder_prefix":
|
|
|
|
conf.IMAPFolderPrefix = value
|
2021-05-15 11:19:28 +02:00
|
|
|
case "smtp_url":
|
|
|
|
conf.SMTPURL = value
|
|
|
|
case "smtp_username":
|
|
|
|
conf.SMTPUsername = value
|
|
|
|
case "smtp_password":
|
|
|
|
conf.SMTPPassword = value
|
|
|
|
case "to_name":
|
|
|
|
conf.ToName = value
|
|
|
|
case "to_address":
|
|
|
|
conf.ToAddress = value
|
|
|
|
case "from_name":
|
|
|
|
conf.FromName = value
|
|
|
|
case "from_address":
|
|
|
|
conf.FromAddress = value
|
2021-06-25 09:14:27 +02:00
|
|
|
case "local_db_path":
|
|
|
|
conf.LocalDBPath = value
|
2021-05-15 11:19:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
2022-09-14 16:14:18 +02:00
|
|
|
func NewFromEnvironment() *Configuration {
|
|
|
|
days, _ := strconv.Atoi(os.Getenv("GTE_DAYS_AHEAD"))
|
|
|
|
|
|
|
|
return &Configuration{
|
|
|
|
IMAPURL: os.Getenv("IMAP_URL"),
|
|
|
|
IMAPUsername: os.Getenv("IMAP_USER"),
|
|
|
|
IMAPPassword: os.Getenv("IMAP_PASSWORD"),
|
|
|
|
IMAPFolderPrefix: os.Getenv("IMAP_FOLDER_PREFIX"),
|
|
|
|
SMTPURL: os.Getenv("SMTP_URL"),
|
|
|
|
SMTPUsername: os.Getenv("SMTP_USER"),
|
|
|
|
SMTPPassword: os.Getenv("SMTP_PASSWORD"),
|
|
|
|
ToName: os.Getenv("GTE_TO_NAME"),
|
|
|
|
ToAddress: os.Getenv("GTE_TO_ADDRESS"),
|
|
|
|
FromName: os.Getenv("GTE_FROM_NAME"),
|
|
|
|
FromAddress: os.Getenv("GTE_FROM_ADDRESS"),
|
|
|
|
DaysAhead: days,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 11:19:28 +02:00
|
|
|
func (c *Configuration) IMAP() *mstore.IMAPConfig {
|
|
|
|
return &mstore.IMAPConfig{
|
2021-09-22 07:11:05 +02:00
|
|
|
IMAPURL: c.IMAPURL,
|
|
|
|
IMAPUsername: c.IMAPUsername,
|
|
|
|
IMAPPassword: c.IMAPPassword,
|
|
|
|
IMAPFolderPrefix: c.IMAPFolderPrefix,
|
2021-05-15 11:19:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) SMTP() *msend.SSLSMTPConfig {
|
|
|
|
return &msend.SSLSMTPConfig{
|
|
|
|
URL: c.SMTPURL,
|
|
|
|
Username: c.SMTPUsername,
|
|
|
|
Password: c.SMTPPassword,
|
|
|
|
From: c.FromAddress,
|
|
|
|
To: c.ToAddress,
|
|
|
|
}
|
|
|
|
}
|
2021-06-25 09:14:27 +02:00
|
|
|
|
|
|
|
func (c *Configuration) Sqlite() *storage.SqliteConfig {
|
|
|
|
return &storage.SqliteConfig{
|
|
|
|
DBPath: c.LocalDBPath,
|
|
|
|
}
|
|
|
|
}
|