Parse account configuration

This commit is contained in:
Drew DeVault 2018-01-09 21:24:50 -05:00
parent 6394e386c2
commit b5d5e0dbed
4 changed files with 83 additions and 15 deletions

View file

@ -10,19 +10,28 @@ import (
func main() {
var (
c *config.AercConfig
conf *config.AercConfig
err error
)
if c, err = config.LoadConfig(nil); err != nil {
if conf, err = config.LoadConfig(nil); err != nil {
panic(err)
}
fmt.Printf("%+v\n", *c)
w := worker.NewWorker("")
go w.Run()
w.PostAction(types.Ping{})
workers := make([]worker.Worker, 0)
for _, account := range conf.Accounts {
var work worker.Worker
if work, err = worker.NewWorker(account.Source); err != nil {
panic(err)
}
fmt.Printf("Initializing worker %s\n", account.Name)
go work.Run()
work.PostAction(types.Configure{Config: account})
workers = append(workers, work)
}
for {
if msg := w.GetMessage(); msg != nil {
fmt.Printf("<- %T: %v\n", msg, msg)
for _, worker := range workers {
if msg := worker.GetMessage(); msg != nil {
fmt.Printf("<- %T\n", msg)
}
}
}
}

View file

@ -4,7 +4,9 @@ import (
"github.com/go-ini/ini"
"github.com/kyoh86/xdg"
"fmt"
"path"
"strings"
"unicode"
)
@ -47,6 +49,43 @@ func mapName(raw string) string {
return string(newstr)
}
func loadAccountConfig(path string) ([]AccountConfig, error) {
var (
file *ini.File
err error
accounts []AccountConfig
)
accounts = make([]AccountConfig, 0)
if file, err = ini.Load(path); err != nil {
return nil, err
}
file.NameMapper = mapName
for _, _sec := range file.SectionStrings() {
if _sec == "DEFAULT" {
continue
}
sec := file.Section(_sec)
account := AccountConfig{Name: _sec}
if err = sec.MapTo(&account); err != nil {
return nil, err
}
for key, val := range sec.KeysHash() {
if key == "source" {
account.Source = val
} else if key == "folders" {
account.Folders = strings.Split(val, ",")
} else if key != "name" {
account.Params[key] = val
}
}
if account.Source == "" {
return nil, fmt.Errorf("Expected source for account %s", _sec)
}
accounts = append(accounts, account)
}
return accounts, nil
}
func LoadConfig(root *string) (*AercConfig, error) {
var (
err error
@ -80,5 +119,11 @@ func LoadConfig(root *string) (*AercConfig, error) {
if ui, err := file.GetSection("ui"); err != nil {
ui.MapTo(config.Ui)
}
accountsPath := path.Join(*root, "accounts.conf")
if accounts, err := loadAccountConfig(accountsPath); err != nil {
return nil, err
} else {
config.Accounts = accounts
}
return config, nil
}

View file

@ -34,11 +34,11 @@ func (w *IMAPWorker) PostAction(msg types.WorkerMessage) {
func (w *IMAPWorker) handleMessage(_msg types.WorkerMessage) {
switch msg := _msg.(type) {
case types.Ping:
w.messages <- &types.Ack{
w.messages <- types.Ack{
Message: types.RespondTo(msg),
}
default:
w.messages <- &types.Unsupported{
w.messages <- types.Unsupported{
Message: types.RespondTo(_msg),
}
}
@ -49,7 +49,7 @@ func (w *IMAPWorker) Run() {
for {
select {
case msg := <-w.actions:
fmt.Printf("<= %T: %+v\n", msg, msg)
fmt.Printf("<= %T\n", msg)
w.handleMessage(msg)
default:
// no-op

View file

@ -3,6 +3,9 @@ package worker
import (
"git.sr.ht/~sircmpwn/aerc2/worker/imap"
"git.sr.ht/~sircmpwn/aerc2/worker/types"
"fmt"
"net/url"
)
type Worker interface {
@ -12,7 +15,18 @@ type Worker interface {
}
// Guesses the appropriate worker type based on the given source string
func NewWorker(source string) Worker {
// TODO: Do this properly
return imap.NewIMAPWorker()
func NewWorker(source string) (Worker, error) {
var (
u *url.URL
err error
)
if u, err = url.Parse(source); err != nil {
return nil, err
}
switch u.Scheme {
case "imap":
case "imaps":
return imap.NewIMAPWorker(), nil
}
return nil, fmt.Errorf("Unknown backend %s", u.Scheme)
}