diff --git a/Selector.CLI/Program.cs b/Selector.CLI/Program.cs index 578b6b3..fcbfcc3 100644 --- a/Selector.CLI/Program.cs +++ b/Selector.CLI/Program.cs @@ -1,5 +1,7 @@ using System; using System.Threading.Tasks; +using System.CommandLine; +using System.CommandLine.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -18,98 +20,127 @@ namespace Selector.CLI { 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(); } - static IHostBuilder CreateHostBuilder(string[] args) + public static RootOptions ConfigureOptions(HostBuilderContext context, IServiceCollection services) + { + services.Configure(options => + { + OptionsHelper.ConfigureOptions(options, context.Configuration); + }); + + return OptionsHelper.ConfigureOptions(context.Configuration); + } + + public static void ConfigureLastFm(RootOptions config, IServiceCollection services) + { + if(config.LastfmClient is not null) + { + Console.WriteLine("> Adding Last.fm credentials..."); + + var lastAuth = new LastAuth(config.LastfmClient, config.LastfmSecret); + services.AddSingleton(lastAuth); + } + else + { + Console.WriteLine("> No Last.fm credentials, skipping init..."); + } + } + + public static void ConfigureDb(RootOptions config, IServiceCollection services) + { + if (config.DatabaseOptions.Enabled) + { + Console.WriteLine("> Adding Databse Context..."); + services.AddDbContext(options => + options.UseNpgsql(config.DatabaseOptions.ConnectionString) + ); + } + } + + public static void ConfigureRedis(RootOptions config, IServiceCollection services) + { + if (config.RedisOptions.Enabled) + { + Console.WriteLine("> Configuring Redis..."); + + if(string.IsNullOrWhiteSpace(config.RedisOptions.ConnectionString)) + { + Console.WriteLine("> No Redis configuration string provided, exiting..."); + Environment.Exit(1); + } + + var connMulti = ConnectionMultiplexer.Connect(config.RedisOptions.ConnectionString); + services.AddSingleton(connMulti); + services.AddTransient(services => services.GetService().GetDatabase()); + services.AddTransient(services => services.GetService().GetSubscriber()); + } + } + + public static void ConfigureEqual(RootOptions config, IServiceCollection services) + { + switch (config.Equality) + { + case EqualityChecker.Uri: + Console.WriteLine("> Using Uri Equality"); + services.AddSingleton(); + break; + case EqualityChecker.String: + Console.WriteLine("> Using String Equality"); + services.AddSingleton(); + 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(); + services.AddSingleton(); + services.AddSingleton(); + // For generating spotify clients + //services.AddSingleton(); + services.AddSingleton(); + + ConfigureLastFm(config, services); + ConfigureDb(config, services); + ConfigureRedis(config, services); + ConfigureEqual(config, services); + + // HOSTED SERVICES + if (config.WatcherOptions.Enabled) + { + Console.WriteLine("> Adding Watcher Service"); + services.AddHostedService(); + } + } + + public static void ConfigureDefaultNlog(HostBuilderContext context, ILoggingBuilder builder) + { + builder.ClearProviders(); + builder.SetMinimumLevel(LogLevel.Trace); + builder.AddNLog(context.Configuration); + } + + static IHostBuilder CreateHostBuilder(string[] args, Action BuildServices, Action BuildLogs) => Host.CreateDefaultBuilder(args) - .ConfigureServices((context, services) => - { - Console.WriteLine("~~~ Selector CLI ~~~"); - Console.WriteLine(); - - Console.WriteLine("> Configuring..."); - // CONFIG - services.Configure(options => - { - OptionsHelper.ConfigureOptions(options, context.Configuration); - }); - var config = OptionsHelper.ConfigureOptions(context.Configuration); - - Console.WriteLine("> Adding Services..."); - // SERVICES - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - // For generating spotify clients - //services.AddSingleton(); - services.AddSingleton(); - - if(config.LastfmClient is not null) - { - Console.WriteLine("> Adding Last.fm credentials..."); - - var lastAuth = new LastAuth(config.LastfmClient, config.LastfmSecret); - services.AddSingleton(lastAuth); - } - else - { - Console.WriteLine("> No Last.fm credentials, skipping init..."); - } - - // DB - if (config.DatabaseOptions.Enabled) - { - Console.WriteLine("> Adding Databse Context..."); - services.AddDbContext(options => - options.UseNpgsql(config.DatabaseOptions.ConnectionString) - ); - } - - // REDIS - if (config.RedisOptions.Enabled) - { - Console.WriteLine("> Configuring Redis..."); - - if(string.IsNullOrWhiteSpace(config.RedisOptions.ConnectionString)) - { - Console.WriteLine("> No Redis configuration string provided, exiting..."); - Environment.Exit(1); - } - - var connMulti = ConnectionMultiplexer.Connect(config.RedisOptions.ConnectionString); - services.AddSingleton(connMulti); - services.AddTransient(services => services.GetService().GetDatabase()); - services.AddTransient(services => services.GetService().GetSubscriber()); - } - - // EQUAL - switch (config.Equality) - { - case EqualityChecker.Uri: - Console.WriteLine("> Using Uri Equality"); - services.AddTransient(); - break; - case EqualityChecker.String: - Console.WriteLine("> Using String Equality"); - services.AddTransient(); - break; - } - - // HOSTED SERVICES - if (config.WatcherOptions.Enabled) - { - Console.WriteLine("> Adding Watcher Service"); - services.AddHostedService(); - } - - }) - .ConfigureLogging((context, builder) => - { - builder.ClearProviders(); - builder.SetMinimumLevel(LogLevel.Trace); - builder.AddNLog(context.Configuration); - }); + .ConfigureServices((context, services) => BuildServices(context, services)) + .ConfigureLogging((context, builder) => BuildLogs(context, builder)); } } diff --git a/Selector.CLI/Selector.CLI.csproj b/Selector.CLI/Selector.CLI.csproj index 8e51ddd..8dcbd4a 100644 --- a/Selector.CLI/Selector.CLI.csproj +++ b/Selector.CLI/Selector.CLI.csproj @@ -15,6 +15,8 @@ + + diff --git a/Selector.Cache/Key.cs b/Selector.Cache/Key.cs index 3242b3c..a1fdab1 100644 --- a/Selector.Cache/Key.cs +++ b/Selector.Cache/Key.cs @@ -10,6 +10,8 @@ namespace Selector.Cache public const string TrackName = "Track"; public const string AudioFeatureName = "AudioFeature"; + public const string WorkerName = "Worker"; + public static string CurrentlyPlaying(string user) => Namespace(new[] { user, CurrentlyPlayingName }); public static string AudioFeature(string trackId) => Namespace(new[] { TrackName, trackId, AudioFeatureName });