118 lines
4.1 KiB
C#
118 lines
4.1 KiB
C#
7 years ago
|
using System;
|
||
|
using System.Diagnostics;
|
||
|
using System.Threading.Tasks;
|
||
|
using Microsoft.AspNet.Hosting;
|
||
|
using Microsoft.AspNet.Hosting.Server;
|
||
|
using Microsoft.AspNet.Identity;
|
||
|
using Microsoft.Extensions.CommandLineUtils;
|
||
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
using Microsoft.Extensions.Logging;
|
||
|
using Microsoft.Extensions.PlatformAbstractions;
|
||
|
using Yavsc.Models;
|
||
|
using Yavsc.Services;
|
||
|
|
||
|
namespace cli
|
||
|
{
|
||
|
|
||
|
public class Program
|
||
|
{
|
||
|
private readonly EventLog _log =
|
||
|
new EventLog("Application") { Source = "Application" };
|
||
|
|
||
|
public Program()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public static void Main(string[] args)
|
||
|
{
|
||
|
var prog = new Program();
|
||
|
|
||
|
// Program.exe <-g|--greeting|-$ <greeting>> [name <fullname>]
|
||
|
// [-?|-h|--help] [-u|--uppercase]
|
||
|
CommandLineApplication commandLineApplication =
|
||
|
new CommandLineApplication(throwOnUnexpectedArg: false);
|
||
|
CommandArgument names = null;
|
||
|
commandLineApplication.Command("name",
|
||
|
(target) =>
|
||
|
names = target.Argument(
|
||
|
"fullname",
|
||
|
"Enter the full name of the person to be greeted.",
|
||
|
multipleValues: true));
|
||
|
CommandOption greeting = commandLineApplication.Option(
|
||
|
"-$|-g |--greeting <greeting>",
|
||
|
"The greeting to display. The greeting supports"
|
||
|
+ " a format string where {fullname} will be "
|
||
|
+ "substituted with the full name.",
|
||
|
CommandOptionType.SingleValue);
|
||
|
CommandOption envNameOption = commandLineApplication.Option(
|
||
|
"-e |--environment <environment>","The environment to run against.",CommandOptionType.SingleValue);
|
||
|
CommandOption uppercase = commandLineApplication.Option(
|
||
|
"-u | --uppercase", "Display the greeting in uppercase.",
|
||
|
CommandOptionType.NoValue);
|
||
|
commandLineApplication.HelpOption("-? | -h | --help");
|
||
|
commandLineApplication.OnExecute(() =>
|
||
|
{
|
||
|
string greetingString = "Hello!";
|
||
|
string environmentName = "Production";
|
||
|
if (greeting.HasValue())
|
||
|
{
|
||
|
|
||
|
string greetingStringTemplate = greeting.Value();
|
||
|
|
||
|
var fullname = string.Join(" ", commandLineApplication.RemainingArguments);
|
||
|
greetingString = greetingStringTemplate.Replace("{fullname}", fullname);
|
||
|
}
|
||
|
if (envNameOption.HasValue()){
|
||
|
environmentName = envNameOption.Value();
|
||
|
}
|
||
|
Greet(greetingString, environmentName);
|
||
|
return 0;
|
||
|
});
|
||
|
commandLineApplication.Execute(args);
|
||
|
}
|
||
|
|
||
|
private static void Greet(
|
||
|
string greeting, string environmentName)
|
||
|
{
|
||
|
Console.WriteLine(greeting);
|
||
|
IServiceCollection services = new ServiceCollection();
|
||
|
// Startup.cs finally :)
|
||
|
IHostingEnvironment hosting = new HostingEnvironment{ EnvironmentName = environmentName };
|
||
|
var test = PlatformServices.Create (null);
|
||
|
var basePath = AppDomain.CurrentDomain.BaseDirectory;
|
||
|
// FIXME null ref var appName = AppDomain.CurrentDomain.ApplicationIdentity.FullName;
|
||
|
|
||
|
PlatformServices.SetDefault (test);
|
||
|
|
||
|
|
||
|
Startup startup = new Startup(hosting, null);
|
||
|
startup.ConfigureServices(services);
|
||
|
IServiceProvider serviceProvider = services.BuildServiceProvider();
|
||
|
|
||
|
//configure console logging
|
||
|
serviceProvider
|
||
|
.GetService<ILoggerFactory>()
|
||
|
.AddConsole(LogLevel.Debug);
|
||
|
|
||
|
var logger = serviceProvider.GetService<ILoggerFactory>()
|
||
|
.CreateLogger<Program>();
|
||
|
|
||
|
logger.LogDebug("Logger is working!");
|
||
|
// Get Service and call method
|
||
|
var userManager = serviceProvider.GetService<UserManager<ApplicationUser> >();
|
||
|
var emailSender = serviceProvider.GetService<IEmailSender>();
|
||
|
|
||
|
foreach (var user in userManager.Users)
|
||
|
{
|
||
|
Task.Run(async () =>
|
||
|
await emailSender.SendEmailAsync(Startup.SiteSetup, Startup.SmtpSettup, Startup.SiteSetup.Owner.Name, Startup.SiteSetup.Owner.EMail,
|
||
|
$"[{Startup.SiteSetup.Title}] Rappel de votre inscription ({user.UserName})", $"{user.Id}/{user.UserName}/{user.Email}"));
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|