From aa725a083e43a959b48b49513479698727a3678b Mon Sep 17 00:00:00 2001 From: "Martin Hinshelwood nkdAgility.com" Date: Thu, 25 Jul 2024 13:39:12 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20(CommandSettingsBase.cs,=20Migratio?= =?UTF-8?q?nToolHost.cs):=20add=20support=20for=20command-line=20config=20?= =?UTF-8?q?file=20option=20=E2=AC=86=EF=B8=8F=20(MigrationTools.Host.cspro?= =?UTF-8?q?j):=20add=20System.CommandLine=20package?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add support for specifying the configuration file via command-line options `--config`, `--configFile`, or `-c`. This enhances flexibility by allowing users to specify a custom configuration file when running the application. The `System.CommandLine` package is added to facilitate this functionality. The `ForceGetConfigFile` method is introduced to parse the command-line arguments and extract the configuration file path. The `CreateDefaultBuilder` method in `MigrationToolHost.cs` is updated to use this configuration file path, ensuring the application loads the correct configuration. --- .../Commands/CommandSettingsBase.cs | 25 ++++--- src/MigrationTools.Host/MigrationToolHost.cs | 72 +++++++++---------- .../MigrationTools.Host.csproj | 1 + 3 files changed, 54 insertions(+), 44 deletions(-) diff --git a/src/MigrationTools.Host/Commands/CommandSettingsBase.cs b/src/MigrationTools.Host/Commands/CommandSettingsBase.cs index 397d47f5d..fe6071e9b 100644 --- a/src/MigrationTools.Host/Commands/CommandSettingsBase.cs +++ b/src/MigrationTools.Host/Commands/CommandSettingsBase.cs @@ -1,20 +1,15 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Text; -using System.Xml.Serialization; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; +using System.ComponentModel; using Newtonsoft.Json; using Spectre.Console.Cli; using YamlDotNet.Serialization; +using System.CommandLine; namespace MigrationTools.Host.Commands { internal class CommandSettingsBase : CommandSettings { [Description("Pre configure paramiters using this config file. Run `Init` to create it.")] - [CommandOption("--config|--configFile")] + [CommandOption("--config|--configFile|-c")] [DefaultValue("configuration.json")] [JsonIgnore, YamlIgnore] public string ConfigFile { get; set; } @@ -27,5 +22,19 @@ internal class CommandSettingsBase : CommandSettings [CommandOption("--skipVersionCheck")] public bool skipVersionCheck { get; set; } + public static string ForceGetConfigFile(string[] args) + { + var fileOption = new Option("--config"); + fileOption.AddAlias("-c"); + fileOption.AddAlias("--configFile"); + + var rootCommand = new RootCommand(); + rootCommand.AddOption(fileOption); + + var file = rootCommand.Parse(args); + return file.GetValueForOption(fileOption); + + } } + } diff --git a/src/MigrationTools.Host/MigrationToolHost.cs b/src/MigrationTools.Host/MigrationToolHost.cs index 71a3be507..9e40eb1f4 100644 --- a/src/MigrationTools.Host/MigrationToolHost.cs +++ b/src/MigrationTools.Host/MigrationToolHost.cs @@ -21,6 +21,7 @@ using Spectre.Console.Cli.Extensions.DependencyInjection; using Spectre.Console.Cli; using Serilog.Filters; +using MigrationTools.Host.Commands; namespace MigrationTools.Host { @@ -30,6 +31,8 @@ public static class MigrationToolHost public static IHostBuilder CreateDefaultBuilder(string[] args) { + var configFile = CommandSettingsBase.ForceGetConfigFile(args); + var hostBuilder = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args); hostBuilder.UseSerilog((hostingContext, services, loggerConfiguration) => @@ -59,43 +62,40 @@ public static IHostBuilder CreateDefaultBuilder(string[] args) hostBuilder.ConfigureLogging((context, logBuilder) => { - }); - //.ConfigureAppConfiguration(builder => - //{ - // if (executeOptions is not null) - // { - // builder.AddJsonFile(executeOptions.ConfigFile); - // } - //}) + }) + .ConfigureAppConfiguration(builder => + { + builder.AddJsonFile(configFile); + }); hostBuilder.ConfigureServices((context, services) => { services.AddOptions(); - //services.Configure((config) => - //{ - // 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; - //}); - - + services.Configure((config) => + { + var sp = services.BuildServiceProvider(); + var logger = sp.GetService().CreateLogger(); + if (!File.Exists(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", 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(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(); @@ -122,7 +122,7 @@ public static IHostBuilder CreateDefaultBuilder(string[] args) // Host Services services.AddTransient(); - + }); hostBuilder.ConfigureServices((context, services) => @@ -144,7 +144,7 @@ public static IHostBuilder CreateDefaultBuilder(string[] args) services.AddHostedService(); }); - hostBuilder.UseConsoleLifetime(); + hostBuilder.UseConsoleLifetime(); @@ -181,7 +181,7 @@ public static async Task RunMigrationTools(this IHostBuilder hostBuilder, string await host.RunAsync(); } - static string logDate = DateTime.Now.ToString("yyyyMMddHHmmss"); + static string logDate = DateTime.Now.ToString("yyyyMMddHHmmss"); private static string CreateLogsPath() { diff --git a/src/MigrationTools.Host/MigrationTools.Host.csproj b/src/MigrationTools.Host/MigrationTools.Host.csproj index 97a7c8baa..966f7654b 100644 --- a/src/MigrationTools.Host/MigrationTools.Host.csproj +++ b/src/MigrationTools.Host/MigrationTools.Host.csproj @@ -33,6 +33,7 @@ +