yavsc/src/cli/Program.cs

141 lines
5.1 KiB
C#
Raw Normal View History

2018-07-28 01:55:13 +02:00

using System;
2019-06-25 01:51:23 +01:00
using System.Runtime.Versioning;
2018-08-05 04:16:21 +02:00
using cli.Commands;
2019-06-25 01:51:23 +01:00
using Microsoft.AspNet.Builder.Internal;
using Microsoft.AspNet.Hosting;
using Microsoft.Dnx.Runtime;
2018-07-28 01:55:13 +02:00
using Microsoft.Extensions.CommandLineUtils;
2019-06-25 01:51:23 +01:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel;
using Microsoft.Extensions.PlatformAbstractions;
using Newtonsoft.Json;
using Yavsc;
2018-07-28 01:55:13 +02:00
namespace cli
{
2019-06-25 01:51:23 +01:00
public class CliAppEnv : IApplicationEnvironment
{
public CliAppEnv(IApplicationEnvironment defaultEnv)
{
var envAppVar = Environment.GetEnvironmentVariable("ASPNET_ENV");
Configuration = envAppVar ?? defaultEnv.Configuration;
ApplicationName = defaultEnv.ApplicationName;
ApplicationVersion = defaultEnv.ApplicationVersion;
ApplicationBasePath = defaultEnv.ApplicationBasePath;
RuntimeFramework = defaultEnv.RuntimeFramework;
}
public string ApplicationName { get; private set; }
public string ApplicationVersion { get; private set; }
public string ApplicationBasePath { get; private set; }
public string Configuration { get; private set; }
public FrameworkName RuntimeFramework { get; private set; }
public object GetData(string name)
{
throw new NotImplementedException();
}
public void SetData(string name, object value)
{
throw new NotImplementedException();
}
}
2018-07-28 01:55:13 +02:00
public partial class Program
{
2019-06-25 01:51:23 +01:00
private static IApplicationEnvironment appEnv;
public static IHostingEnvironment HostingEnvironment { get; private set; }
public Program()
2018-07-28 01:55:13 +02:00
{
2019-06-25 01:51:23 +01:00
appEnv = new CliAppEnv(PlatformServices.Default.Application);
}
2018-07-28 01:55:13 +02:00
2019-06-25 01:51:23 +01:00
/// <summary>
/// Initializes the application by
/// </summary>
private static ApplicationBuilder ConfigureApplication()
{
AppDomain.CurrentDomain.UnhandledException += OnUnHandledException;
var services = new ServiceCollection();
// create a service provider with the HostEnvironment.
2020-09-12 01:11:30 +01:00
HostingEnvironment = new HostingEnvironment
{
EnvironmentName = appEnv.Configuration
};
2019-06-25 01:51:23 +01:00
var startup = new Startup(HostingEnvironment, appEnv);
startup.ConfigureServices(services);
services.AddInstance<IHostingEnvironment>(HostingEnvironment);
var serviceProvider = services.BuildServiceProvider();
2020-09-12 01:11:30 +01:00
var app = new ApplicationBuilder(serviceProvider)
{
ApplicationServices = serviceProvider
};
2019-06-25 01:51:23 +01:00
var siteSettings = serviceProvider.GetRequiredService<IOptions<SiteSettings>>();
var cxSettings = serviceProvider.GetRequiredService<IOptions<ConnectionSettings>>();
var userCxSettings = serviceProvider.GetRequiredService<IOptions<UserConnectionSettings>>();
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
2020-09-12 01:11:30 +01:00
startup.Configure(cxSettings, userCxSettings, loggerFactory);
2019-06-25 01:51:23 +01:00
return app;
}
private static void OnUnHandledException(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine("Unhandled Exception occured:");
Console.WriteLine(JsonConvert.SerializeObject(e.ExceptionObject));
}
[STAThread]
public static int Main(string[] args)
{
2020-09-12 01:11:30 +01:00
CommandLineApplication cliapp = new CommandLineApplication(false)
{
Name = "cli",
FullName = "Yavsc command line interface",
Description = "Dnx console app for yavsc server side",
ShortVersionGetter = () => "v1.0",
LongVersionGetter = () => "version 1.0 (stable)"
};
2019-06-25 01:51:23 +01:00
// calling a Startup sequence
var appBuilder = ConfigureApplication();
var loggerFactory = appBuilder.ApplicationServices.GetRequiredService<ILoggerFactory>();
2019-06-25 17:11:16 +01:00
var cxSettings = appBuilder.ApplicationServices.GetRequiredService<IOptions<ConnectionSettings>>();
var usercxSettings = appBuilder.ApplicationServices.GetRequiredService<IOptions<UserConnectionSettings>>();
2019-06-25 01:51:23 +01:00
CommandOption rootCommandHelpOption = cliapp.HelpOption("-? | -h | --help");
2018-08-05 04:16:21 +02:00
new SendMailCommandProvider().Integrate(cliapp);
new GenerateJsonSchema().Integrate(cliapp);
2020-12-29 13:02:31 +00:00
new AuthCommander(loggerFactory).Integrate(cliapp);
new GenerationCommander().Integrate(cliapp);
new Streamer(loggerFactory, cxSettings, usercxSettings ).Integrate(cliapp);
2021-06-03 18:21:31 +01:00
new UserListCleanUp().Integrate(cliapp);
2018-07-28 01:55:13 +02:00
if (args.Length == 0)
{
cliapp.ShowHint();
2019-06-25 01:51:23 +01:00
return -1;
2018-07-28 01:55:13 +02:00
}
2019-06-25 01:51:23 +01:00
var result = cliapp.Execute(args);
2018-07-28 01:55:13 +02:00
if (cliapp.RemainingArguments.Count > 0)
{
cliapp.ShowHint();
2019-06-25 01:51:23 +01:00
return -1;
2018-07-28 01:55:13 +02:00
}
2019-06-25 01:51:23 +01:00
return result;
2018-07-28 01:55:13 +02:00
}
}
}