diff --git a/src/MigrationTools.ConsoleCore/Properties/launchSettings.json b/src/MigrationTools.ConsoleCore/Properties/launchSettings.json index 0ad86d657..3c30516d4 100644 --- a/src/MigrationTools.ConsoleCore/Properties/launchSettings.json +++ b/src/MigrationTools.ConsoleCore/Properties/launchSettings.json @@ -3,6 +3,9 @@ "MigrationTools.ConsoleUI": { "commandName": "Project", "commandLineArgs": "execute --config \"configuration.json\"" + }, + "empty": { + "commandName": "Project" } } } \ No newline at end of file diff --git a/src/MigrationTools.Host/Commands/MigrationExecuteCommand.cs b/src/MigrationTools.Host/Commands/MigrationExecuteCommand.cs index 81f95fc76..49e4664af 100644 --- a/src/MigrationTools.Host/Commands/MigrationExecuteCommand.cs +++ b/src/MigrationTools.Host/Commands/MigrationExecuteCommand.cs @@ -18,7 +18,7 @@ internal class MigrationExecuteCommand : AsyncCommand logger, + ILogger logger, IHostApplicationLifetime appLifetime, ITelemetryLogger telemetryLogger) { Telemetery = telemetryLogger; diff --git a/src/MigrationTools.Host/Commands/MigrationInitCommand.cs b/src/MigrationTools.Host/Commands/MigrationInitCommand.cs index 084096c1b..1ce50c035 100644 --- a/src/MigrationTools.Host/Commands/MigrationInitCommand.cs +++ b/src/MigrationTools.Host/Commands/MigrationInitCommand.cs @@ -23,7 +23,7 @@ internal class MigrationInitCommand : AsyncCommand public MigrationInitCommand( IEngineConfigurationBuilder configurationBuilder, ISettingsWriter settingsWriter, - ILogger logger, + ILogger logger, ITelemetryLogger telemetryLogger, IHostApplicationLifetime appLifetime) { @@ -35,7 +35,7 @@ public MigrationInitCommand( } - public override Task ExecuteAsync(CommandContext context, MigrationInitCommandSettings settings) + public override async Task ExecuteAsync(CommandContext context, MigrationInitCommandSettings settings) { int _exitCode; try diff --git a/src/MigrationTools.Host/HostExtensions.cs b/src/MigrationTools.Host/HostExtensions.cs index ac26aace5..9e07b8294 100644 --- a/src/MigrationTools.Host/HostExtensions.cs +++ b/src/MigrationTools.Host/HostExtensions.cs @@ -1,6 +1,5 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using MigrationTools.Host.CommandLine; namespace MigrationTools.Host { diff --git a/src/MigrationTools.Host/MigrationService.cs b/src/MigrationTools.Host/MigrationService.cs index d12b97bcb..d4b2f69ff 100644 --- a/src/MigrationTools.Host/MigrationService.cs +++ b/src/MigrationTools.Host/MigrationService.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -25,7 +26,7 @@ public MigrationService(ICommandApp appCommand, IHostApplicationLifetime appLife protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - await AppCommand.RunAsync(Environment.GetCommandLineArgs()); + await AppCommand.RunAsync(Environment.GetCommandLineArgs().Skip(1)); AppLifetime.StopApplication(); } } diff --git a/src/MigrationTools.Host/MigrationToolHost.cs b/src/MigrationTools.Host/MigrationToolHost.cs index 5d31e84bc..ce5033e5f 100644 --- a/src/MigrationTools.Host/MigrationToolHost.cs +++ b/src/MigrationTools.Host/MigrationToolHost.cs @@ -18,6 +18,8 @@ using Serilog.Core; using Serilog.Events; using Serilog.Sinks.SystemConsole.Themes; +using Spectre.Console.Cli.Extensions.DependencyInjection; +using Spectre.Console.Cli; namespace MigrationTools.Host { @@ -25,75 +27,65 @@ public static class MigrationToolHost { public static IHostBuilder CreateDefaultBuilder(string[] args) { - (var initOptions, var executeOptions) = ParseOptions(args); + var hostBuilder = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args); - if (initOptions is null && executeOptions is null) + hostBuilder.UseSerilog((hostingContext, services, loggerConfiguration) => { - return null; - } - - - - var hostBuilder = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args) - .UseSerilog((hostingContext, services, loggerConfiguration) => + string outputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] [" + GetVersionTextForLog() + "] {Message:lj}{NewLine}{Exception}"; + string logsPath = CreateLogsPath(); + var logPath = Path.Combine(logsPath, "migration.log"); + var logLevel = hostingContext.Configuration.GetValue("LogLevel"); + var levelSwitch = new LoggingLevelSwitch(logLevel); + loggerConfiguration + .MinimumLevel.ControlledBy(levelSwitch) + .ReadFrom.Configuration(hostingContext.Configuration) + .Enrich.FromLogContext() + .Enrich.WithMachineName() + .Enrich.WithProcessId() + .WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Debug, theme: AnsiConsoleTheme.Code, outputTemplate: outputTemplate) + .WriteTo.ApplicationInsights(services.GetService(), new CustomConverter(), LogEventLevel.Error) + .WriteTo.File(logPath, LogEventLevel.Verbose, outputTemplate: outputTemplate); + }); + + hostBuilder.ConfigureLogging((context, logBuilder) => { - string outputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] [" + GetVersionTextForLog() + "] {Message:lj}{NewLine}{Exception}"; - string logsPath = CreateLogsPath(); - var logPath = Path.Combine(logsPath, "migration.log"); - var logLevel = hostingContext.Configuration.GetValue("LogLevel"); - var levelSwitch = new LoggingLevelSwitch(logLevel); - loggerConfiguration - .MinimumLevel.ControlledBy(levelSwitch) - .ReadFrom.Configuration(hostingContext.Configuration) - .Enrich.FromLogContext() - .Enrich.WithMachineName() - .Enrich.WithProcessId() - .WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Debug, theme: AnsiConsoleTheme.Code, outputTemplate: outputTemplate) - .WriteTo.ApplicationInsights(services.GetService(), new CustomConverter(), LogEventLevel.Error) - .WriteTo.File(logPath, LogEventLevel.Verbose, outputTemplate: outputTemplate); - }) - .ConfigureLogging((context, logBuilder) => - { - }) - .ConfigureAppConfiguration(builder => - { - if (executeOptions is not null) - { - builder.AddJsonFile(executeOptions.ConfigFile); - } - }) - .ConfigureServices((context, services) => + }); + //.ConfigureAppConfiguration(builder => + //{ + // if (executeOptions is not null) + // { + // builder.AddJsonFile(executeOptions.ConfigFile); + // } + //}) + + hostBuilder.ConfigureServices((context, services) => { services.AddOptions(); services.Configure((config) => { - if(executeOptions is null) - { - return; - } - var sp = services.BuildServiceProvider(); var logger = sp.GetService().CreateLogger(); - if (!File.Exists(executeOptions.ConfigFile)) - { - logger.LogInformation("The config file {ConfigFile} does not exist, nor does the default 'configuration.json'. Use '{ExecutableName}.exe init' to create a configuration file first", executeOptions.ConfigFile, Assembly.GetEntryAssembly().GetName().Name); - throw new ArgumentException("missing configfile"); - } - logger.LogInformation("Config Found, creating engine host"); - var reader = sp.GetRequiredService(); - var parsed = reader.BuildFromFile(executeOptions.ConfigFile); - config.ChangeSetMappingFile = parsed.ChangeSetMappingFile; - config.FieldMaps = parsed.FieldMaps; - config.GitRepoMapping = parsed.GitRepoMapping; - config.CommonEnrichersConfig = parsed.CommonEnrichersConfig; - config.Processors = parsed.Processors; - config.Source = parsed.Source; - config.Target = parsed.Target; - config.Version = parsed.Version; - config.workaroundForQuerySOAPBugEnabled = parsed.workaroundForQuerySOAPBugEnabled; - config.WorkItemTypeDefinition = parsed.WorkItemTypeDefinition; + //if (!File.Exists(executeOptions.ConfigFile)) + //{ + // logger.LogInformation("The config file {ConfigFile} does not exist, nor does the default 'configuration.json'. Use '{ExecutableName}.exe init' to create a configuration file first", executeOptions.ConfigFile, Assembly.GetEntryAssembly().GetName().Name); + // throw new ArgumentException("missing configfile"); + //} + //logger.LogInformation("Config Found, creating engine host"); + //var reader = sp.GetRequiredService(); + //var parsed = reader.BuildFromFile(executeOptions.ConfigFile); + //config.ChangeSetMappingFile = parsed.ChangeSetMappingFile; + //config.FieldMaps = parsed.FieldMaps; + //config.GitRepoMapping = parsed.GitRepoMapping; + //config.CommonEnrichersConfig = parsed.CommonEnrichersConfig; + //config.Processors = parsed.Processors; + //config.Source = parsed.Source; + //config.Target = parsed.Target; + //config.Version = parsed.Version; + //config.workaroundForQuerySOAPBugEnabled = parsed.workaroundForQuerySOAPBugEnabled; + //config.WorkItemTypeDefinition = parsed.WorkItemTypeDefinition; }); + // Application Insights ApplicationInsightsServiceOptions aiso = new ApplicationInsightsServiceOptions(); aiso.ApplicationVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); @@ -102,7 +94,7 @@ public static IHostBuilder CreateDefaultBuilder(string[] args) //aiso.DeveloperMode = true; //#endif services.AddApplicationInsightsTelemetryWorkerService(aiso); - + // Services services.AddTransient(); //services.AddTransient(); @@ -120,36 +112,30 @@ public static IHostBuilder CreateDefaultBuilder(string[] args) // Host Services services.AddTransient(); - if (initOptions is not null) - { - services.Configure((opts) => - { - opts.ConfigFile = initOptions.ConfigFile; - opts.Options = initOptions.Options; - }); - services.AddHostedService(); - } - if (executeOptions is not null) - { - services.Configure(cred => - { - cred.Source = new Credentials - { - Domain = executeOptions.SourceDomain, - UserName = executeOptions.SourceUserName, - Password = executeOptions.SourcePassword - }; - cred.Target = new Credentials - { - Domain = executeOptions.TargetDomain, - UserName = executeOptions.TargetUserName, - Password = executeOptions.TargetPassword - }; - }); - services.AddHostedService(); - } - }) - .UseConsoleLifetime(); + + }); + + hostBuilder.ConfigureServices((context, services) => + { + using var registrar = new DependencyInjectionRegistrar(services); + var app = new CommandApp(registrar); + app.Configure(config => + { + config.PropagateExceptions(); + config.AddCommand("execute"); + config.AddCommand("init"); + + }); + services.AddSingleton(app); + }); + + hostBuilder.ConfigureServices((context, services) => + { + services.AddHostedService(); + }); + + hostBuilder.UseConsoleLifetime(); + return hostBuilder; @@ -173,19 +159,14 @@ public static async Task RunMigrationTools(this IHostBuilder hostBuilder, string // Disanle telemitery from options - (var initOptions, var executeOptions) = ParseOptions(args); - if (initOptions is null && executeOptions is null) - { - return; - } - bool DisableTelemetry = false; - Serilog.ILogger logger = host.Services.GetService(); - if (executeOptions is not null && bool.TryParse(executeOptions.DisableTelemetry, out DisableTelemetry)) - { - TelemetryConfiguration ai = host.Services.GetService(); - ai.DisableTelemetry = DisableTelemetry; - } - logger.Information("Telemetry: {status}", !DisableTelemetry); + //bool DisableTelemetry = false; + //Serilog.ILogger logger = host.Services.GetService(); + //if (executeOptions is not null && bool.TryParse(executeOptions.DisableTelemetry, out DisableTelemetry)) + //{ + // TelemetryConfiguration ai = host.Services.GetService(); + // ai.DisableTelemetry = DisableTelemetry; + //} + //logger.Information("Telemetry: {status}", !DisableTelemetry); await host.RunAsync(); } @@ -202,21 +183,5 @@ private static string CreateLogsPath() return exportPath; } - - private static (InitOptions init, ExecuteOptions execute) ParseOptions(string[] args) - { - InitOptions initOptions = null; - ExecuteOptions executeOptions = null; - Parser.Default.ParseArguments(args) - .WithParsed(opts => - { - initOptions = opts; - }) - .WithParsed(opts => - { - executeOptions = opts; - }); - return (initOptions, executeOptions); - } } } \ No newline at end of file diff --git a/src/MigrationTools.Host/MigrationTools.Host.csproj b/src/MigrationTools.Host/MigrationTools.Host.csproj index 4b84da626..97a7c8baa 100644 --- a/src/MigrationTools.Host/MigrationTools.Host.csproj +++ b/src/MigrationTools.Host/MigrationTools.Host.csproj @@ -32,6 +32,7 @@ +