From 42d518ee670af1f4133129f4380d37c3af89d2e6 Mon Sep 17 00:00:00 2001 From: Maxwell Weru Date: Sun, 2 Jun 2024 15:59:06 +0300 Subject: [PATCH] Setup config values at the start and pass as argument where possible --- .../Client/FaluClientConfigureOptions.cs | 13 ------------ .../IServiceCollectionExtensions.cs | 21 +++++++++---------- .../Extensions/InvocationContextExtensions.cs | 1 - src/FaluCli/Program.cs | 14 ++++++++----- 4 files changed, 19 insertions(+), 30 deletions(-) delete mode 100644 src/FaluCli/Client/FaluClientConfigureOptions.cs diff --git a/src/FaluCli/Client/FaluClientConfigureOptions.cs b/src/FaluCli/Client/FaluClientConfigureOptions.cs deleted file mode 100644 index b109fbb8..00000000 --- a/src/FaluCli/Client/FaluClientConfigureOptions.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Falu.Config; -using Microsoft.Extensions.Options; - -namespace Falu.Client; - -internal class FaluClientConfigureOptions(IConfigValuesProvider configValuesProvider) : IConfigureOptions -{ - public void Configure(FaluClientOptions options) - { - var values = configValuesProvider.GetConfigValuesAsync().GetAwaiter().GetResult(); - options.Retries = values.Retries; - } -} diff --git a/src/FaluCli/Extensions/IServiceCollectionExtensions.cs b/src/FaluCli/Extensions/IServiceCollectionExtensions.cs index 32165a05..50b5556c 100644 --- a/src/FaluCli/Extensions/IServiceCollectionExtensions.cs +++ b/src/FaluCli/Extensions/IServiceCollectionExtensions.cs @@ -11,15 +11,19 @@ internal static class IServiceCollectionExtensions { // services are registered as transit to allow for easier debugging because no scope is created by the parser - public static IServiceCollection AddFaluClientForCli(this IServiceCollection services) + public static IServiceCollection AddFaluClientForCli(this IServiceCollection services, ConfigValues configValues) { - // A dummy ApiKey is used so that the options validator can pass - services.AddFalu(o => o.ApiKey = "dummy") - .AddHttpMessageHandler() - .ConfigureHttpClientStandard(); + var builder = services.AddFalu(options => + { + // A dummy ApiKey is used so that the options validator can pass + options.ApiKey = "dummy"; + options.Retries = configValues.Retries; + }); + + builder.AddHttpMessageHandler() + .ConfigureHttpClientStandard(); services.AddTransient(); - services.ConfigureOptions(); return services; } @@ -32,11 +36,6 @@ public static IServiceCollection AddUpdateChecker(this IServiceCollection servic return services.AddSingleton(p => p.GetRequiredService()); } - public static IServiceCollection AddConfigValuesProvider(this IServiceCollection services) - { - return services.AddTransient(); - } - public static IServiceCollection AddOpenIdProvider(this IServiceCollection services) { services.AddHttpClient(name: "Oidc") diff --git a/src/FaluCli/Extensions/InvocationContextExtensions.cs b/src/FaluCli/Extensions/InvocationContextExtensions.cs index d6020ed0..51e1d2a3 100644 --- a/src/FaluCli/Extensions/InvocationContextExtensions.cs +++ b/src/FaluCli/Extensions/InvocationContextExtensions.cs @@ -8,7 +8,6 @@ namespace System.CommandLine; internal static class InvocationContextExtensions { public static bool IsVerboseEnabled(this InvocationContext context) => context.ParseResult.ValueForOption("--verbose"); - public static bool IsTelemetryDisabled(this InvocationContext context) => context.ParseResult.ValueForOption("--no-telemetry"); public static string? GetWorkspaceId(this InvocationContext context) => context.ParseResult.ValueForOption("--workspace"); public static bool? GetLiveMode(this InvocationContext context) => context.ParseResult.ValueForOption("--live"); diff --git a/src/FaluCli/Program.cs b/src/FaluCli/Program.cs index 83e4eced..154ac582 100644 --- a/src/FaluCli/Program.cs +++ b/src/FaluCli/Program.cs @@ -7,6 +7,7 @@ using Falu.Commands.Money.Statements; using Falu.Commands.RequestLogs; using Falu.Commands.Templates; +using Falu.Config; using System.CommandLine.Builder; using System.CommandLine.Hosting; using Res = Falu.Properties.Resources; @@ -72,15 +73,18 @@ rootCommand.Description = "Official CLI tool for Falu."; rootCommand.AddGlobalOption(["-v", "--verbose"], "Whether to output verbosely.", false); -rootCommand.AddGlobalOption(["--skip-update-checks"], Res.OptionDescriptionSkipUpdateCheck); // nullable so as to allow checking if specified +rootCommand.AddGlobalOption(["--skip-update-checks"], Res.OptionDescriptionSkipUpdateCheck); // nullable so as to allow checking if not specified + +var configValuesProvider = new ConfigValuesProvider(); +var configValues = await configValuesProvider.GetConfigValuesAsync(); var builder = new CommandLineBuilder(rootCommand) .UseHost(_ => Host.CreateDefaultBuilder(args), host => { host.ConfigureAppConfiguration((context, builder) => { - var iv = context.GetInvocationContext(); - var verbose = iv.IsVerboseEnabled(); + var invocation = context.GetInvocationContext(); + var verbose = invocation.IsVerboseEnabled(); builder.AddInMemoryCollection(new Dictionary { @@ -113,9 +117,9 @@ host.ConfigureServices((context, services) => { var configuration = context.Configuration; - services.AddFaluClientForCli(); + services.AddSingleton(configValuesProvider); + services.AddFaluClientForCli(configValues); services.AddUpdateChecker(); - services.AddConfigValuesProvider(); services.AddOpenIdProvider(); services.AddTransient(); });