yavsc/src/cli/Commands/SendMailCommand.cs

61 lines
2.3 KiB
C#
Raw Normal View History

2018-07-28 01:55:13 +02:00
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Hosting;
2018-07-28 01:55:13 +02:00
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;
2021-06-03 16:24:33 +01:00
using Yavsc.Server.Settings;
2018-07-28 01:55:13 +02:00
2021-05-30 12:30:39 +01:00
namespace cli
{
public class SendMailCommandProvider : ICommander
{
2018-08-05 04:16:21 +02:00
public CommandLineApplication Integrate(CommandLineApplication rootApp)
2018-07-28 01:55:13 +02:00
{
2021-05-30 12:30:39 +01:00
CommandArgument critCommandArg = null;
CommandOption sendHelpOption = null;
CommandLineApplication sendMailCommandApp
2021-06-03 18:21:31 +01:00
= rootApp.Command("send-email",
2021-05-30 12:30:39 +01:00
(target) =>
{
target.FullName = "Send email";
2021-06-03 18:21:31 +01:00
target.Description = "Sends emails using given template from code";
2021-05-30 12:30:39 +01:00
sendHelpOption = target.HelpOption("-? | -h | --help");
critCommandArg = target.Argument(
"criteria",
"user selection criteria : 'allow-monthly' or 'email-not-confirmed'");
}, false);
2018-07-28 01:55:13 +02:00
sendMailCommandApp.OnExecute(() =>
{
2021-06-03 16:24:33 +01:00
bool showhelp = !UserPolicies.Criterias.ContainsKey(critCommandArg.Value)
|| sendHelpOption.HasValue();
2021-05-30 12:30:39 +01:00
if (!showhelp)
2018-07-28 01:55:13 +02:00
{
var host = new WebHostBuilder();
2021-05-30 12:30:39 +01:00
var hostengnine = host.UseEnvironment(Program.HostingEnvironment.EnvironmentName)
2018-07-28 01:55:13 +02:00
.UseServer("cli")
.UseStartup<Startup>()
.Build();
var app = hostengnine.Start();
var mailer = app.Services.GetService<EMailer>();
var loggerFactory = app.Services.GetService<ILoggerFactory>();
2021-05-30 12:30:39 +01:00
var logger = loggerFactory.CreateLogger<Program>();
2018-07-28 01:55:13 +02:00
logger.LogInformation("Starting emailling");
2021-06-03 16:24:33 +01:00
mailer.SendEmailFromCriteria(critCommandArg.Value);
2018-07-28 01:55:13 +02:00
logger.LogInformation("Finished emailling");
}
else
{
sendMailCommandApp.ShowHelp();
2019-01-08 15:00:58 +00:00
return 1;
2018-07-28 01:55:13 +02:00
}
return 0;
});
return sendMailCommandApp;
}
}
2021-05-30 12:30:39 +01:00
}