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() { func main() {
var ( var (
c *config.AercConfig conf *config.AercConfig
err error err error
) )
if c, err = config.LoadConfig(nil); err != nil { if conf, err = config.LoadConfig(nil); err != nil {
panic(err) panic(err)
} }
fmt.Printf("%+v\n", *c) workers := make([]worker.Worker, 0)
w := worker.NewWorker("") for _, account := range conf.Accounts {
go w.Run() var work worker.Worker
w.PostAction(types.Ping{}) 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 { for {
if msg := w.GetMessage(); msg != nil { for _, worker := range workers {
fmt.Printf("<- %T: %v\n", msg, msg) 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/go-ini/ini"
"github.com/kyoh86/xdg" "github.com/kyoh86/xdg"
"fmt"
"path" "path"
"strings"
"unicode" "unicode"
) )
@ -47,6 +49,43 @@ func mapName(raw string) string {
return string(newstr) 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) { func LoadConfig(root *string) (*AercConfig, error) {
var ( var (
err error err error
@ -80,5 +119,11 @@ func LoadConfig(root *string) (*AercConfig, error) {
if ui, err := file.GetSection("ui"); err != nil { if ui, err := file.GetSection("ui"); err != nil {
ui.MapTo(config.Ui) 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 return config, nil
} }

View file

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

View file

@ -3,6 +3,9 @@ package worker
import ( import (
"git.sr.ht/~sircmpwn/aerc2/worker/imap" "git.sr.ht/~sircmpwn/aerc2/worker/imap"
"git.sr.ht/~sircmpwn/aerc2/worker/types" "git.sr.ht/~sircmpwn/aerc2/worker/types"
"fmt"
"net/url"
) )
type Worker interface { type Worker interface {
@ -12,7 +15,18 @@ type Worker interface {
} }
// Guesses the appropriate worker type based on the given source string // Guesses the appropriate worker type based on the given source string
func NewWorker(source string) Worker { func NewWorker(source string) (Worker, error) {
// TODO: Do this properly var (
return imap.NewIMAPWorker() 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)
} }