|
@@ -14,14 +14,39 @@ import (
|
|
|
|
|
|
var ErrPortOutOfRange = errors.New("port out of range")
|
|
|
var ErrMissingPassword = errors.New("password has not been set")
|
|
|
+var ErrMissingDefaultAccount = errors.New("default_account is missing from config")
|
|
|
|
|
|
// CastingErrors is a simple wrapper around map[string]error. It's endowed with a utility
|
|
|
// function to check if there are any errors.
|
|
|
-type CastingErrors map[string]error
|
|
|
+type CastingErrors struct {
|
|
|
+ GeneralErrors []error
|
|
|
+ AccountErrors map[string]error
|
|
|
+}
|
|
|
+
|
|
|
+func initCastingErrors() *CastingErrors {
|
|
|
+ c := CastingErrors{}
|
|
|
+ c.AccountErrors = make(map[string]error)
|
|
|
+ return &c
|
|
|
+}
|
|
|
+
|
|
|
+func (c *CastingErrors) appendGeneralError(e error) {
|
|
|
+ c.GeneralErrors = append(c.GeneralErrors, e)
|
|
|
+}
|
|
|
+
|
|
|
+func (c *CastingErrors) String() string {
|
|
|
+ out := "CastingErrors{"
|
|
|
+ out += fmt.Sprintf("GeneralErrors: %s, ", c.GeneralErrors)
|
|
|
+ out += fmt.Sprintf("AccountErrors: %s", c.AccountErrors)
|
|
|
+ out += "}"
|
|
|
+ return out
|
|
|
+}
|
|
|
|
|
|
// Check if there are any errors, i.e. if any value of the map is not nil.
|
|
|
func (c CastingErrors) Check() bool {
|
|
|
- for _, err := range c {
|
|
|
+ if len(c.GeneralErrors) != 0 {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ for _, err := range c.GeneralErrors {
|
|
|
if err != nil {
|
|
|
return false
|
|
|
}
|
|
@@ -32,6 +57,7 @@ func (c CastingErrors) Check() bool {
|
|
|
// MemConfig is the data structure that will be used program-wise. It holds all the
|
|
|
// necessary information to authenticate and manage each provided account.
|
|
|
type MemConfig struct {
|
|
|
+ Default string
|
|
|
Accounts map[string]*AccountData
|
|
|
Workers map[string]*worker.Worker
|
|
|
}
|
|
@@ -68,18 +94,23 @@ func (a *AccountData) String() string {
|
|
|
}
|
|
|
|
|
|
// parseConfig translates a *fileConfig, as obtained from a file read, into a usable
|
|
|
-// *MemConfig that could be used in computation, together with a custom CastingErrors
|
|
|
+// *MemConfig that could be used in computation, together with a custom *CastingErrors
|
|
|
// type, that holds the translation errors for each account.
|
|
|
-func parseConfig(fileConfig *fileConfig) (*MemConfig, CastingErrors) {
|
|
|
+func parseConfig(fileConfig *fileConfig) (*MemConfig, *CastingErrors) {
|
|
|
+ errors := initCastingErrors()
|
|
|
outConfig := initMemConfig()
|
|
|
+ defaultAccount := fileConfig.Default
|
|
|
+ if defaultAccount == "" {
|
|
|
+ errors.appendGeneralError(ErrMissingDefaultAccount)
|
|
|
+ }
|
|
|
+ outConfig.Default = defaultAccount
|
|
|
basePath, err := getOrDefaultMailbox(fileConfig.MailboxPath)
|
|
|
defaultMessages := getOrDefaultMessages(fileConfig.DefaultMessages)
|
|
|
if err != nil {
|
|
|
log.Fatal("Could not determine base path")
|
|
|
}
|
|
|
- errors := make(map[string]error)
|
|
|
for _, account := range fileConfig.Accounts {
|
|
|
- outConfig.Accounts[account.Name], errors[account.Name] = parseData(&account, basePath, defaultMessages)
|
|
|
+ outConfig.Accounts[account.Name], errors.AccountErrors[account.Name] = parseData(&account, basePath, defaultMessages)
|
|
|
outConfig.Workers[account.Name] = &worker.Worker{}
|
|
|
}
|
|
|
return &outConfig, errors
|
|
@@ -165,7 +196,6 @@ func getPassword(connection *connectionInfo) (string, error) {
|
|
|
}
|
|
|
|
|
|
func getMailboxPath(name, configPath, basePath string) string {
|
|
|
- log.Printf("name: %s,\tconfig_path: %s,\tbase_path: %s\n", name, configPath, basePath)
|
|
|
if configPath != "" {
|
|
|
if info, err := os.Stat(configPath); err == nil || err != os.ErrNotExist && info != nil {
|
|
|
if info.IsDir() {
|