package main import ( "os" cfg "mailsrv/config" srv "mailsrv/services" "github.com/alecthomas/kingpin/v2" "github.com/rs/zerolog" "github.com/rs/zerolog/log" ini "gopkg.in/ini.v1" ) const DefaultOutboxPath string = "outbox" var iniPath = kingpin.Arg("ini", ".ini file path").Required().String() func LoadIni(iniPath string) (*ini.File, error) { ini, err := ini.Load(iniPath) if err != nil { return nil, err } return ini, nil } // LoadSMTPConfig collects mandatory SMTP parameters to send an e-mail from the `.ini` file func LoadSMTPConfig(iniFile *ini.File) (cfg.SMTPConfig, error) { var config cfg.SMTPConfig section := iniFile.Section("server") config, err := cfg.NewSMTPConfig( section.Key("username").String(), section.Key("password").String(), section.Key("url").String(), section.Key("port").String(), ) if err != nil { return config, err } return config, nil } // GetOutBoxPath tries to get the outbox path from the `.ini` file and creates the directory // if the path does not exist, create a default one: `outbox` next to the binary func GetOutboxPath(iniFile *ini.File) (string, error) { outboxPath := iniFile.Section("service").Key("outbox_path").String() if outboxPath == "" { outboxPath = DefaultOutboxPath } if err := os.MkdirAll(outboxPath, 0750); err != nil && !os.IsExist(err) { return "", err } return outboxPath, nil } func main() { zerolog.TimeFieldFormat = zerolog.TimeFormatUnix log.Logger = log.With().Caller().Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr}) kingpin.Parse() iniFile, err := LoadIni(*iniPath) if err != nil { log.Fatal().AnErr("err", err).Msg("unable to load the .ini configuration file") } config, err := LoadSMTPConfig(iniFile) if err != nil { log.Fatal().AnErr("err", err).Msg("unable to load the SMTP configuration") } outboxPath, err := GetOutboxPath(iniFile) if err != nil { log.Fatal().AnErr("err", err).Msg("unable to retrieve outputbox path") return } sender := srv.NewSender(config, outboxPath) sender.Run() }