gte/internal/configuration/configuration.go

108 lines
2.0 KiB
Go
Raw Normal View History

2021-05-15 11:19:28 +02:00
package configuration
import (
"bufio"
"errors"
"io"
"strings"
2021-06-25 09:14:27 +02:00
"time"
2021-05-15 11:19:28 +02:00
2021-09-19 11:59:26 +02:00
"ewintr.nl/gte/internal/storage"
"ewintr.nl/gte/pkg/msend"
"ewintr.nl/gte/pkg/mstore"
2021-05-15 11:19:28 +02:00
)
var (
ErrUnableToRead = errors.New("unable to read configuration")
)
type Configuration struct {
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
}
type LocalConfiguration struct {
MinSyncInterval time.Duration
2021-05-15 11:19:28 +02:00
}
func New(src io.Reader) *Configuration {
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
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
}
func (c *Configuration) IMAP() *mstore.IMAPConfig {
return &mstore.IMAPConfig{
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,
}
}