yavsc/src/cli/Commands/SendMailCommand.cs

55 lines
2.1 KiB
C#
Raw Normal View History

2018-07-28 01:55:13 +02:00
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using cli.Services;
2018-08-05 04:16:21 +02:00
using cli.Model;
2018-07-28 01:55:13 +02:00
namespace cli {
2018-08-05 04:16:21 +02:00
public class SendMailCommandProvider : ICommander {
public CommandLineApplication Integrate(CommandLineApplication rootApp)
2018-07-28 01:55:13 +02:00
{
2018-07-30 11:21:57 +02:00
CommandArgument sendMailCommandArg = null;
CommandOption sendHelpOption = null;
2018-07-28 01:55:13 +02:00
CommandLineApplication sendMailCommandApp
= rootApp.Command("send",
(target) =>
{
target.FullName = "Send email";
target.Description = "Sends emails using given template";
2018-07-30 11:21:57 +02:00
sendHelpOption = target.HelpOption("-? | -h | --help");
sendMailCommandArg = target.Argument(
2018-07-28 01:55:13 +02:00
"class",
"class name of mailling to execute (actually, only 'monthly') .",
multipleValues: true);
}, false);
sendMailCommandApp.OnExecute(() =>
{
2018-07-30 11:21:57 +02:00
if (sendMailCommandArg.Value == "monthly")
2018-07-28 01:55:13 +02:00
{
var host = new WebHostBuilder();
var hostengnine = host.UseEnvironment("Development")
.UseServer("cli")
.UseStartup<Startup>()
.Build();
var app = hostengnine.Start();
var mailer = app.Services.GetService<EMailer>();
var loggerFactory = app.Services.GetService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger<cli.Program>();
logger.LogInformation("Starting emailling");
mailer.SendMonthlyEmail(1, "UserOrientedTemplate");
logger.LogInformation("Finished emailling");
}
else
{
sendMailCommandApp.ShowHelp();
}
return 0;
});
return sendMailCommandApp;
}
}
}