Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relay CLI - global options #33

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions app/Hutch.Relay/Commands/Helpers/CliMiddlewareExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,52 @@ public static CommandLineBuilder UseRootCommandBypass(this CommandLineBuilder cl
/// </summary>
/// <param name="cli"></param>
/// <param name="args"></param>
/// <param name="configureHost"></param>
/// <returns></returns>
public static CommandLineBuilder UseCliHost(this CommandLineBuilder cli, string[] args, Func<HostApplicationBuilder, Task> configureHost)
public static CommandLineBuilder UseCliHost(this CommandLineBuilder cli, string[] args,
Func<HostApplicationBuilder, Task> configureHost)
{
// We used to build the host in advance, but now that we use it for full DI registration
// We don't want to build it in the case of RootCommandBypass
// So instead we build it inside the middleware,
// only if we reach it in the middleware pipeline,
// before handling a given Command

return cli.AddMiddleware(async (context, next) =>
{
//-- Modify Host Builder creation

var hostBuilderSettings = new HostApplicationBuilderSettings
{
Args = args // pass on the cli args
};

// Override environment with the global cli option
var environment = context.ParseResult.CommandResult
.GetValueForOption(CliRootCommand.OptEnvironment);
if (environment is not null)
hostBuilderSettings.EnvironmentName = environment;

// We create a generic host to load config and bootstrap stuff "for free"
var builder = Host.CreateApplicationBuilder(args);
var builder = Host.CreateApplicationBuilder(hostBuilderSettings);


//-- Introduce extra configuration


// Override connection string with the global cli option
var connectionString = context.ParseResult.CommandResult
.GetValueForOption(CliRootCommand.OptConnectionString);
if (connectionString is not null)
builder.Configuration.AddInMemoryCollection([
new("ConnectionStrings:Default", connectionString)
]);

// allow for external configuration to be applied
// (primarily DI service registration)
await configureHost.Invoke(builder);


var host = builder.Build();

// Then we use the middleware to add scoped access to the host's service catalog,
Expand Down
6 changes: 6 additions & 0 deletions app/Hutch.Relay/Services/DbManagementService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Hutch.Relay.Services;

public class DbManagementService
{

}
11 changes: 10 additions & 1 deletion app/Hutch.Relay/Startup/Cli/CliRootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ namespace Hutch.Relay.Startup.Cli;

public class CliRootCommand : RootCommand
{
public static readonly Option<string?> OptEnvironment =
new(["--environment", "-e"],
"Override the application host's Environment Name.");

public static readonly Option<string?> OptConnectionString =
new(["--connection-string"],
"Override the local datastore connection string.");

public CliRootCommand() : base("Hutch Relay")
{
AddGlobalOption(new Option<string>(new[] { "--environment", "-e" }));
AddGlobalOption(OptEnvironment);
AddGlobalOption(OptConnectionString);

// Add Commands here
AddCommand(new("users", "Relay User actions")
Expand Down
1 change: 0 additions & 1 deletion app/Hutch.Relay/Startup/Cli/ConfigureCliServices.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.CommandLine.Builder;
using Hutch.Relay.Commands.Runners;
using Hutch.Relay.Constants;
using Hutch.Relay.Data;
Expand Down