Skip to content
This repository has been archived by the owner on Dec 25, 2024. It is now read-only.

Commit

Permalink
Setup config values at the start and pass as argument where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell committed Jun 2, 2024
1 parent ae9b30e commit 42d518e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
13 changes: 0 additions & 13 deletions src/FaluCli/Client/FaluClientConfigureOptions.cs

This file was deleted.

21 changes: 10 additions & 11 deletions src/FaluCli/Extensions/IServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FaluCliClient, FaluClientOptions>(o => o.ApiKey = "dummy")
.AddHttpMessageHandler<FaluCliClientHandler>()
.ConfigureHttpClientStandard();
var builder = services.AddFalu<FaluCliClient, FaluClientOptions>(options =>
{
// A dummy ApiKey is used so that the options validator can pass
options.ApiKey = "dummy";
options.Retries = configValues.Retries;
});

builder.AddHttpMessageHandler<FaluCliClientHandler>()
.ConfigureHttpClientStandard();

services.AddTransient<FaluCliClientHandler>();
services.ConfigureOptions<FaluClientConfigureOptions>();

return services;
}
Expand All @@ -32,11 +36,6 @@ public static IServiceCollection AddUpdateChecker(this IServiceCollection servic
return services.AddSingleton<IHostedService>(p => p.GetRequiredService<UpdateChecker>());
}

public static IServiceCollection AddConfigValuesProvider(this IServiceCollection services)
{
return services.AddTransient<IConfigValuesProvider, ConfigValuesProvider>();
}

public static IServiceCollection AddOpenIdProvider(this IServiceCollection services)
{
services.AddHttpClient<OidcProvider>(name: "Oidc")
Expand Down
1 change: 0 additions & 1 deletion src/FaluCli/Extensions/InvocationContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace System.CommandLine;
internal static class InvocationContextExtensions
{
public static bool IsVerboseEnabled(this InvocationContext context) => context.ParseResult.ValueForOption<bool>("--verbose");
public static bool IsTelemetryDisabled(this InvocationContext context) => context.ParseResult.ValueForOption<bool>("--no-telemetry");
public static string? GetWorkspaceId(this InvocationContext context) => context.ParseResult.ValueForOption<string>("--workspace");
public static bool? GetLiveMode(this InvocationContext context) => context.ParseResult.ValueForOption<bool?>("--live");

Expand Down
14 changes: 9 additions & 5 deletions src/FaluCli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,15 +73,18 @@

rootCommand.Description = "Official CLI tool for Falu.";
rootCommand.AddGlobalOption(["-v", "--verbose"], "Whether to output verbosely.", false);
rootCommand.AddGlobalOption<bool?>(["--skip-update-checks"], Res.OptionDescriptionSkipUpdateCheck); // nullable so as to allow checking if specified
rootCommand.AddGlobalOption<bool?>(["--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<string, string?>
{
Expand Down Expand Up @@ -113,9 +117,9 @@
host.ConfigureServices((context, services) =>
{
var configuration = context.Configuration;
services.AddFaluClientForCli();
services.AddSingleton<IConfigValuesProvider>(configValuesProvider);
services.AddFaluClientForCli(configValues);
services.AddUpdateChecker();
services.AddConfigValuesProvider();
services.AddOpenIdProvider();
services.AddTransient<WebsocketHandler>();
});
Expand Down

0 comments on commit 42d518e

Please sign in to comment.