refactoring of code to make it more readable

This commit is contained in:
Michael 2022-05-07 03:57:16 +01:00
parent c4a60e0796
commit e516bab46a
Signed by: michael
GPG Key ID: 523BD9EF68BDD44C
4 changed files with 46 additions and 67 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ logs
msgs msgs
config.yaml config.yaml
go.sum go.sum
.vscode

View File

@ -10,16 +10,7 @@ import (
// Config application configuration // Config application configuration
type Config struct { type Config struct {
Path string `yaml:"path"` Path string `yaml:"path"`
Account []struct { Account []Account
Addr string `yaml:"addr"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
Proxy struct {
Enabled bool `yaml:"enabled"`
Addr string `yaml:"addr"`
Proto string `yaml:"proto"`
}
Logger struct { Logger struct {
Path string `yaml:"path"` Path string `yaml:"path"`
Mode string `yaml:"mode"` Mode string `yaml:"mode"`
@ -27,6 +18,12 @@ type Config struct {
} }
} }
type Account struct {
Addr string `yaml:"addr"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
// NewConfig - create new config instance from file path // NewConfig - create new config instance from file path
func NewConfig(configFile string) (config *Config) { func NewConfig(configFile string) (config *Config) {
file, err := ioutil.ReadFile(configFile) file, err := ioutil.ReadFile(configFile)

View File

@ -44,11 +44,7 @@ func LoginWithProxy(conn Conn, network string, proxyAddress string, auth *proxy.
if err != nil { if err != nil {
return nil, err return nil, err
} }
mail, err = login(conn, client) return login(conn, client)
if err != nil {
return nil, err
}
return mail, nil
} }
func Login(conn Conn) (mail *Mail, err error) { func Login(conn Conn) (mail *Mail, err error) {
@ -56,11 +52,7 @@ func Login(conn Conn) (mail *Mail, err error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
mail, err = login(conn, client) return login(conn, client)
if err != nil {
return nil, err
}
return mail, nil
} }
func (mail *Mail) ListMailboxes() { func (mail *Mail) ListMailboxes() {
@ -109,10 +101,7 @@ func (mail *Mail) GetMessages(mailbox string, path string) error {
return err return err
} }
} }
if err := <-done; err != nil { return <-done
return err
}
return nil
} }
func Write(path string, msg *imap.Message, literal imap.Literal) error { func Write(path string, msg *imap.Message, literal imap.Literal) error {

64
main.go
View File

@ -13,51 +13,35 @@ import (
func main() { func main() {
cfgFile, mailbox, list := ui() cfgFile, mailbox, list := ui()
cfg := NewConfig(*cfgFile) cfg := NewConfig(*cfgFile)
chErr, chDone := make(chan error), make(chan bool) ch := make(chan error)
for _, account := range cfg.Account { for _, account := range cfg.Account {
go func(addr, username, password string, chErr chan<- error, chDone chan<- bool) { go getmail(account, cfg.Path, *mailbox, *list, ch)
defer func() {
chDone <- true
}()
auth := mail.Conn{Addr: addr, Username: username, Password: password}
var conn *mail.Mail
var err error
if cfg.Proxy.Enabled {
conn, err = mail.LoginWithProxy(auth, cfg.Proxy.Proto, cfg.Proxy.Addr, nil)
if err != nil {
chErr <- err
} }
} else { for c := 0; c < len(cfg.Account); c++ {
conn, err = mail.Login(auth) if <-ch != nil {
if err != nil { log.Println(<-ch)
chErr <- err
} }
} }
}
func getmail(account Account, ph, mailbox string, list bool, ch chan error) {
conn, err := mail.Login(mail.Conn{Addr: account.Addr, Username: account.Username, Password: account.Password})
if err != nil {
ch <- err
return
}
defer conn.Client.Logout() defer conn.Client.Logout()
if !list {
if !*list { path := ph + account.Username + "/" + time.Now().Format("2006-01-02-15:04:05")
path := cfg.Path + username + "/" + time.Now().Format("2006-01-02-15:04:05") makePath(ph+account.Username, path)
for _, p := range []string{cfg.Path + username, path} { if err = conn.GetMessages(mailbox, path); err != nil {
if _, err := os.Stat(p); os.IsNotExist(err) { ch <- err
os.Mkdir(p, 0775)
}
}
if err = conn.GetMessages(*mailbox, path); err != nil {
chErr <- err
} }
ch <- nil
return return
} }
conn.ListMailboxes() conn.ListMailboxes()
}(account.Addr, account.Username, account.Password, chErr, chDone) ch <- nil
}
for c := 0; c < len(cfg.Account); {
select {
case err := <-chErr:
log.Println(err)
case <-chDone:
c++
}
}
} }
func ui() (cfgFile, mailbox *string, list *bool) { func ui() (cfgFile, mailbox *string, list *bool) {
@ -76,3 +60,11 @@ func ui() (cfgFile, mailbox *string, list *bool) {
flag.Parse() flag.Parse()
return cfgFile, mailbox, list return cfgFile, mailbox, list
} }
func makePath(paths ...string) {
for _, p := range paths {
if _, err := os.Stat(p); os.IsNotExist(err) {
os.Mkdir(p, 0775)
}
}
}