62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package config
|
|
|
|
import (
|
|
"flag"
|
|
|
|
ini "gopkg.in/ini.v1"
|
|
|
|
"git.paulbsd.com/paulbsd/getimap/utils"
|
|
)
|
|
|
|
// GetConfig fetch the configuration from ini file
|
|
func GetConfig(configfile string) *Config {
|
|
flag.Usage = utils.Usage
|
|
flag.StringVar(&configfile, "configfile", "getimap.ini", "config file to use with getimap section")
|
|
flag.Parse()
|
|
|
|
cfg, err := ini.Load(configfile)
|
|
utils.HandleFatalError(err)
|
|
|
|
var mc = new(Config)
|
|
|
|
imapconfigSection := cfg.Section("imap")
|
|
mc.ImapConfig.Hostname = imapconfigSection.Key("hostname").String()
|
|
mc.ImapConfig.Port, err = imapconfigSection.Key("port").Int()
|
|
mc.ImapConfig.Username = imapconfigSection.Key("username").String()
|
|
mc.ImapConfig.Password = imapconfigSection.Key("password").String()
|
|
mc.ImapConfig.Path = imapconfigSection.Key("path").String()
|
|
mc.ImapConfig.TLS, err = imapconfigSection.Key("tls").Bool()
|
|
utils.HandleError(err)
|
|
|
|
searchconfigSection := cfg.Section("search")
|
|
mc.SearchConfig.From = searchconfigSection.Key("from").String()
|
|
mc.SearchConfig.To = searchconfigSection.Key("to").String()
|
|
mc.SearchConfig.Subject = searchconfigSection.Key("subject").String()
|
|
|
|
outputconfigSection := cfg.Section("output")
|
|
mc.OutputConfig.RegExp = outputconfigSection.Key("regexp").String()
|
|
mc.OutputConfig.Path = outputconfigSection.Key("path").String()
|
|
|
|
return mc
|
|
}
|
|
|
|
type Config struct {
|
|
ImapConfig struct {
|
|
Hostname string
|
|
Port int
|
|
Username string
|
|
Password string
|
|
Path string
|
|
TLS bool
|
|
}
|
|
SearchConfig struct {
|
|
From string
|
|
To string
|
|
Subject string
|
|
}
|
|
OutputConfig struct {
|
|
RegExp string
|
|
Path string
|
|
}
|
|
}
|