diff --git a/main.go b/main.go index 524da35..4191e9e 100644 --- a/main.go +++ b/main.go @@ -13,23 +13,36 @@ func main() { cfgFile, mailbox, msgs := ui() cfg := NewConfig(*cfgFile) logger := logger.New(cfg.Logger.Path, cfg.Logger.Mode, cfg.Logger.Level) - + chErr, chDone := make(chan error), make(chan bool) for _, account := range cfg.Account { - conn, err := mail.Login(cfg.Addr, account.Username, account.Password) - if err != nil { - logger.Error.Fatal(err) - } - defer conn.Client.Logout() - messages, err := conn.GetMessages(*mailbox, int32(*msgs)) - if err != nil { - logger.Error.Fatal(err) - } - path := "./msgs/" + account.Username - if _, err := os.Stat(path); os.IsNotExist(err) { - os.Mkdir(path, 0775) - } - if err := mail.WriteMessages(path, messages); err != nil { - logger.Error.Fatal(err) + go func(username, password string, chErr chan<- error, chDone chan<- bool) { + defer func() { + chDone <- true + }() + conn, err := mail.Login(cfg.Addr, username, password) + if err != nil { + chErr <- err + } + defer conn.Client.Logout() + messages, err := conn.GetMessages(*mailbox, int32(*msgs)) + if err != nil { + chErr <- err + } + path := "./msgs/" + username + if _, err := os.Stat(path); os.IsNotExist(err) { + os.Mkdir(path, 0775) + } + if err := mail.WriteMessages(path, messages); err != nil { + chErr <- err + } + }(account.Username, account.Password, chErr, chDone) + } + for c := 0; c < len(cfg.Account); { + select { + case err := <-chErr: + logger.Error.Error(err) + case <-chDone: + c++ } } }