starting to break up CLI startup, added System.Commandline

This commit is contained in:
andy 2021-11-03 17:38:54 +00:00
parent 3a41a3e5da
commit 933e8725c4
3 changed files with 124 additions and 89 deletions

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.CommandLine;
using System.CommandLine.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -18,34 +20,28 @@ namespace Selector.CLI
{ {
public static async Task Main(string[] args) public static async Task Main(string[] args)
{ {
var host = CreateHostBuilder(args); var cmd = new RootCommand {
new Command("start") {
}
};
var host = CreateHostBuilder(args, ConfigureDefault, ConfigureDefaultNlog);
await host.RunConsoleAsync(); await host.RunConsoleAsync();
} }
static IHostBuilder CreateHostBuilder(string[] args) public static RootOptions ConfigureOptions(HostBuilderContext context, IServiceCollection services)
=> Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{ {
Console.WriteLine("~~~ Selector CLI ~~~");
Console.WriteLine();
Console.WriteLine("> Configuring...");
// CONFIG
services.Configure<RootOptions>(options => services.Configure<RootOptions>(options =>
{ {
OptionsHelper.ConfigureOptions(options, context.Configuration); OptionsHelper.ConfigureOptions(options, context.Configuration);
}); });
var config = OptionsHelper.ConfigureOptions(context.Configuration);
Console.WriteLine("> Adding Services..."); return OptionsHelper.ConfigureOptions(context.Configuration);
// SERVICES }
services.AddSingleton<IWatcherFactory, WatcherFactory>();
services.AddSingleton<IAudioFeatureInjectorFactory, AudioFeatureInjectorFactory>();
services.AddSingleton<IWatcherCollectionFactory, WatcherCollectionFactory>();
// For generating spotify clients
//services.AddSingleton<IRefreshTokenFactoryProvider, RefreshTokenFactoryProvider>();
services.AddSingleton<IRefreshTokenFactoryProvider, CachingRefreshTokenFactoryProvider>();
public static void ConfigureLastFm(RootOptions config, IServiceCollection services)
{
if(config.LastfmClient is not null) if(config.LastfmClient is not null)
{ {
Console.WriteLine("> Adding Last.fm credentials..."); Console.WriteLine("> Adding Last.fm credentials...");
@ -57,8 +53,10 @@ namespace Selector.CLI
{ {
Console.WriteLine("> No Last.fm credentials, skipping init..."); Console.WriteLine("> No Last.fm credentials, skipping init...");
} }
}
// DB public static void ConfigureDb(RootOptions config, IServiceCollection services)
{
if (config.DatabaseOptions.Enabled) if (config.DatabaseOptions.Enabled)
{ {
Console.WriteLine("> Adding Databse Context..."); Console.WriteLine("> Adding Databse Context...");
@ -66,8 +64,10 @@ namespace Selector.CLI
options.UseNpgsql(config.DatabaseOptions.ConnectionString) options.UseNpgsql(config.DatabaseOptions.ConnectionString)
); );
} }
}
// REDIS public static void ConfigureRedis(RootOptions config, IServiceCollection services)
{
if (config.RedisOptions.Enabled) if (config.RedisOptions.Enabled)
{ {
Console.WriteLine("> Configuring Redis..."); Console.WriteLine("> Configuring Redis...");
@ -83,19 +83,45 @@ namespace Selector.CLI
services.AddTransient<IDatabaseAsync>(services => services.GetService<ConnectionMultiplexer>().GetDatabase()); services.AddTransient<IDatabaseAsync>(services => services.GetService<ConnectionMultiplexer>().GetDatabase());
services.AddTransient<ISubscriber>(services => services.GetService<ConnectionMultiplexer>().GetSubscriber()); services.AddTransient<ISubscriber>(services => services.GetService<ConnectionMultiplexer>().GetSubscriber());
} }
}
// EQUAL public static void ConfigureEqual(RootOptions config, IServiceCollection services)
{
switch (config.Equality) switch (config.Equality)
{ {
case EqualityChecker.Uri: case EqualityChecker.Uri:
Console.WriteLine("> Using Uri Equality"); Console.WriteLine("> Using Uri Equality");
services.AddTransient<IEqual, UriEqual>(); services.AddSingleton<IEqual, UriEqual>();
break; break;
case EqualityChecker.String: case EqualityChecker.String:
Console.WriteLine("> Using String Equality"); Console.WriteLine("> Using String Equality");
services.AddTransient<IEqual, StringEqual>(); services.AddSingleton<IEqual, StringEqual>();
break; break;
} }
}
public static void ConfigureDefault(HostBuilderContext context, IServiceCollection services)
{
Console.WriteLine("~~~ Selector CLI ~~~");
Console.WriteLine();
Console.WriteLine("> Configuring...");
// CONFIG
var config = ConfigureOptions(context, services);
Console.WriteLine("> Adding Services...");
// SERVICES
services.AddSingleton<IWatcherFactory, WatcherFactory>();
services.AddSingleton<IAudioFeatureInjectorFactory, AudioFeatureInjectorFactory>();
services.AddSingleton<IWatcherCollectionFactory, WatcherCollectionFactory>();
// For generating spotify clients
//services.AddSingleton<IRefreshTokenFactoryProvider, RefreshTokenFactoryProvider>();
services.AddSingleton<IRefreshTokenFactoryProvider, CachingRefreshTokenFactoryProvider>();
ConfigureLastFm(config, services);
ConfigureDb(config, services);
ConfigureRedis(config, services);
ConfigureEqual(config, services);
// HOSTED SERVICES // HOSTED SERVICES
if (config.WatcherOptions.Enabled) if (config.WatcherOptions.Enabled)
@ -103,13 +129,18 @@ namespace Selector.CLI
Console.WriteLine("> Adding Watcher Service"); Console.WriteLine("> Adding Watcher Service");
services.AddHostedService<WatcherService>(); services.AddHostedService<WatcherService>();
} }
}
}) public static void ConfigureDefaultNlog(HostBuilderContext context, ILoggingBuilder builder)
.ConfigureLogging((context, builder) =>
{ {
builder.ClearProviders(); builder.ClearProviders();
builder.SetMinimumLevel(LogLevel.Trace); builder.SetMinimumLevel(LogLevel.Trace);
builder.AddNLog(context.Configuration); builder.AddNLog(context.Configuration);
}); }
static IHostBuilder CreateHostBuilder(string[] args, Action<HostBuilderContext, IServiceCollection> BuildServices, Action<HostBuilderContext, ILoggingBuilder> BuildLogs)
=> Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) => BuildServices(context, services))
.ConfigureLogging((context, builder) => BuildLogs(context, builder));
} }
} }

View File

@ -15,6 +15,8 @@
<PackageReference Include="NLog" Version="4.7.12" /> <PackageReference Include="NLog" Version="4.7.12" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.7.4" /> <PackageReference Include="NLog.Extensions.Logging" Version="1.7.4" />
<PackageReference Include="SpotifyAPI.Web" Version="6.2.2" /> <PackageReference Include="SpotifyAPI.Web" Version="6.2.2" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.21308.1" />
<PackageReference Include="System.CommandLine.Hosting" Version="0.3.0-alpha.21216.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -10,6 +10,8 @@ namespace Selector.Cache
public const string TrackName = "Track"; public const string TrackName = "Track";
public const string AudioFeatureName = "AudioFeature"; public const string AudioFeatureName = "AudioFeature";
public const string WorkerName = "Worker";
public static string CurrentlyPlaying(string user) => Namespace(new[] { user, CurrentlyPlayingName }); public static string CurrentlyPlaying(string user) => Namespace(new[] { user, CurrentlyPlayingName });
public static string AudioFeature(string trackId) => Namespace(new[] { TrackName, trackId, AudioFeatureName }); public static string AudioFeature(string trackId) => Namespace(new[] { TrackName, trackId, AudioFeatureName });