Skip to content

Instantly share code, notes, and snippets.

@onionhammer
Last active January 21, 2026 20:54
Show Gist options
  • Select an option

  • Save onionhammer/92eaf6ed35e88cf4419d66f76c7d8126 to your computer and use it in GitHub Desktop.

Select an option

Save onionhammer/92eaf6ed35e88cf4419d66f76c7d8126 to your computer and use it in GitHub Desktop.
Custom CLI commands
// Add ReportBuilder -- Process queued reports
var reportBuilder = builder
.AddProject<Projects.C2C_Exchange_ReportBuilder>("c2c-exchange-reportbuilder")
.WithReference(queues)
.WithArgsCommand(
"SendDigestDaily",
"Send Daily Digest",
new CommandOptions() { IconName = "CalendarMail" },
["--digest"]
)
.WithArgsCommand(
"SendDigestWeekly",
"Send Weekly Digest",
new CommandOptions() { IconName = "CalendarMail" },
["--digest", "--weekly"]
)
.WithArgsCommand("ProcessReports", "Process Reports", new CommandOptions() { IconName = "DocumentQueue" }, [])
.WithExplicitStart();
using Microsoft.Extensions.DependencyInjection;
internal static class CommandArgsExtensions
{
public static IResourceBuilder<ProjectResource> WithArgsCommand(
this IResourceBuilder<ProjectResource> builder,
string name,
string displayName,
CommandOptions options,
string[] args
)
{
options.UpdateState = OnUpdateResourceState;
builder.WithCommand(
name: name,
displayName: displayName,
commandOptions: options,
executeCommand: context => OnRunCommandAsync(builder, context, args)
);
return builder;
}
private static async Task<ExecuteCommandResult> OnRunCommandAsync(
IResourceBuilder<ProjectResource> builder,
ExecuteCommandContext context,
string[] args
)
{
var commandService = context.ServiceProvider.GetRequiredService<ResourceCommandService>();
builder.WithArgs(context =>
{
context.Args.Clear();
foreach (var arg in args)
{
context.Args.Add(arg);
}
});
return await commandService.ExecuteCommandAsync(context.ResourceName, "resource-restart");
}
private static ResourceCommandState OnUpdateResourceState(UpdateCommandStateContext context)
{
return ResourceCommandState.Enabled;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment