Skip to content

Commit

Permalink
✨ (CommandSettingsBase.cs, MigrationToolHost.cs): add support for com…
Browse files Browse the repository at this point in the history
…mand-line config file option

⬆️ (MigrationTools.Host.csproj): add System.CommandLine package

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.
  • Loading branch information
MrHinsh committed Jul 25, 2024
1 parent 69f924e commit aa725a0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 44 deletions.
25 changes: 17 additions & 8 deletions src/MigrationTools.Host/Commands/CommandSettingsBase.cs
Original file line number Diff line number Diff line change
@@ -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; }
Expand All @@ -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<string?>("--config");
fileOption.AddAlias("-c");
fileOption.AddAlias("--configFile");

var rootCommand = new RootCommand();
rootCommand.AddOption(fileOption);

var file = rootCommand.Parse(args);
return file.GetValueForOption<string>(fileOption);

}
}

}
72 changes: 36 additions & 36 deletions src/MigrationTools.Host/MigrationToolHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Spectre.Console.Cli.Extensions.DependencyInjection;
using Spectre.Console.Cli;
using Serilog.Filters;
using MigrationTools.Host.Commands;

namespace MigrationTools.Host
{
Expand All @@ -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) =>
Expand Down Expand Up @@ -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<EngineConfiguration>((config) =>
//{
// var sp = services.BuildServiceProvider();
// var logger = sp.GetService<ILoggerFactory>().CreateLogger<EngineConfiguration>();
// //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<IEngineConfigurationReader>();
// //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<EngineConfiguration>((config) =>
{
var sp = services.BuildServiceProvider();
var logger = sp.GetService<ILoggerFactory>().CreateLogger<EngineConfiguration>();
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<IEngineConfigurationReader>();
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();
Expand All @@ -122,7 +122,7 @@ public static IHostBuilder CreateDefaultBuilder(string[] args)
// Host Services
services.AddTransient<IStartupService, StartupService>();
});

hostBuilder.ConfigureServices((context, services) =>
Expand All @@ -144,7 +144,7 @@ public static IHostBuilder CreateDefaultBuilder(string[] args)
services.AddHostedService<MigrationService>();
});

hostBuilder.UseConsoleLifetime();
hostBuilder.UseConsoleLifetime();



Expand Down Expand Up @@ -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()
{
Expand Down
1 change: 1 addition & 0 deletions src/MigrationTools.Host/MigrationTools.Host.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
<PackageReference Include="Spectre.Console.Cli.Extensions.DependencyInjection" Version="0.2.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="WGet.NET" Version="4.1.0" />
<PackageReference Include="YamlDotNet" Version="16.0.0" />
</ItemGroup>
Expand Down

0 comments on commit aa725a0

Please sign in to comment.