using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using StellaOps.BinaryIndex.GoldenSet.Authoring;
using StellaOps.BinaryIndex.GoldenSet.Authoring.Extractors;
namespace StellaOps.BinaryIndex.GoldenSet;
///
/// Extension methods for registering GoldenSet services.
///
public static class GoldenSetServiceCollectionExtensions
{
///
/// Adds GoldenSet services to the dependency injection container.
///
/// The service collection.
/// The configuration.
/// The service collection for chaining.
public static IServiceCollection AddGoldenSetServices(
this IServiceCollection services,
IConfiguration configuration)
{
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(configuration);
// Configuration
services.AddOptions()
.Bind(configuration.GetSection(GoldenSetOptions.SectionName))
.ValidateDataAnnotations()
.ValidateOnStart();
// Core services
services.TryAddSingleton();
services.TryAddSingleton();
// Cross-distro coverage matrix for backport validation
services.TryAddSingleton();
// Memory cache (if not already registered)
services.AddMemoryCache();
return services;
}
///
/// Adds GoldenSet authoring services to the dependency injection container.
///
/// The service collection.
/// The service collection for chaining.
public static IServiceCollection AddGoldenSetAuthoring(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);
// Source extractors
services.TryAddEnumerable(ServiceDescriptor.Singleton());
// Composite extractor
services.TryAddSingleton();
// Upstream commit analyzer
services.TryAddSingleton();
// Enrichment service
services.TryAddScoped();
// Review workflow
services.TryAddScoped();
return services;
}
///
/// Adds PostgreSQL-based GoldenSet storage to the dependency injection container.
///
/// The service collection.
/// The service collection for chaining.
public static IServiceCollection AddGoldenSetPostgresStorage(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);
services.TryAddScoped();
return services;
}
///
/// Adds a CVE validator implementation to the dependency injection container.
///
/// The CVE validator implementation type.
/// The service collection.
/// The service collection for chaining.
public static IServiceCollection AddGoldenSetCveValidator(this IServiceCollection services)
where TValidator : class, ICveValidator
{
ArgumentNullException.ThrowIfNull(services);
services.TryAddSingleton();
return services;
}
}