Skip to content

Instantly share code, notes, and snippets.

@carmeleve
Created August 3, 2020 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carmeleve/1598377a3c4c89ea4a1b6ada4bf79026 to your computer and use it in GitHub Desktop.
Save carmeleve/1598377a3c4c89ea4a1b6ada4bf79026 to your computer and use it in GitHub Desktop.
public static class CliCommandCollectionExtensions
{
public static IServiceCollection AddCliCommands(this IServiceCollection services)
{
Type greetCommandType = typeof(DeployCommand);
Type commandType = typeof(Command);
IEnumerable<Type> commands = greetCommandType
.Assembly
.GetExportedTypes()
.Where(x => x.Namespace == greetCommandType.Namespace && commandType.IsAssignableFrom(x));
foreach (Type command in commands)
{
services.AddSingleton(commandType, command);
}
services.AddSingleton(sp =>
{
return
sp.GetRequiredService<IConfiguration>().GetSection("Deployment").Get<DeploymentOptions>()
?? throw new ArgumentException("Deployment configuration cannot be missing.");
});
return services;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment