Skip to content

Commit

Permalink
Revert "remove variable and build arg paths"
Browse files Browse the repository at this point in the history
This reverts commit 793e54d.
  • Loading branch information
FernandoRojo committed Oct 26, 2024
1 parent 793e54d commit 0c3e448
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ namespace Microsoft.ComponentDetection.Common;
/// <inheritdoc />
public class ComponentDetectionConfigFileService : IComponentDetectionConfigFileService
{
private const string ComponentDetectionConfigFileEnvVar = "ComponentDetection.ComponentDetectionConfigFile";
private readonly IFileUtilityService fileUtilityService;
private readonly IEnvironmentVariableService environmentVariableService;
private readonly IPathUtilityService pathUtilityService;
private readonly ComponentDetectionConfigFile componentDetectionConfig;
private readonly ILogger<FastDirectoryWalkerFactory> logger;
private bool serviceInitComplete;

public ComponentDetectionConfigFileService(
IFileUtilityService fileUtilityService,
IEnvironmentVariableService environmentVariableService,
IPathUtilityService pathUtilityService,
ILogger<FastDirectoryWalkerFactory> logger)
{
this.fileUtilityService = fileUtilityService;
this.pathUtilityService = pathUtilityService;
this.environmentVariableService = environmentVariableService;
this.logger = logger;
this.componentDetectionConfig = new ComponentDetectionConfigFile();
this.serviceInitComplete = false;
Expand All @@ -36,6 +40,12 @@ public ComponentDetectionConfigFile GetComponentDetectionConfig()

public async Task InitAsync(string explicitConfigPath, string rootDirectoryPath = null)
{
await this.LoadFromEnvironmentVariableAsync();
if (!string.IsNullOrEmpty(explicitConfigPath))
{
await this.LoadComponentDetectionConfigAsync(explicitConfigPath);
}

if (!string.IsNullOrEmpty(rootDirectoryPath))
{
await this.LoadComponentDetectionConfigFilesFromRootDirectoryAsync(rootDirectoryPath);
Expand All @@ -55,6 +65,18 @@ private async Task LoadComponentDetectionConfigFilesFromRootDirectoryAsync(strin
}
}

private async Task LoadFromEnvironmentVariableAsync()
{
if (this.environmentVariableService.DoesEnvironmentVariableExist(ComponentDetectionConfigFileEnvVar))
{
var possibleConfigFilePath = this.environmentVariableService.GetEnvironmentVariable(ComponentDetectionConfigFileEnvVar);
if (this.fileUtilityService.Exists(possibleConfigFilePath))
{
await this.LoadComponentDetectionConfigAsync(possibleConfigFilePath);
}
}
}

private async Task LoadComponentDetectionConfigAsync(string configFile)
{
if (!this.fileUtilityService.Exists(configFile))
Expand All @@ -65,9 +87,25 @@ private async Task LoadComponentDetectionConfigAsync(string configFile)
var configFileInfo = new FileInfo(configFile);
var fileContents = await this.fileUtilityService.ReadAllTextAsync(configFileInfo);
var newConfig = this.ParseComponentDetectionConfig(fileContents);
this.MergeComponentDetectionConfig(newConfig);
this.logger.LogInformation("Loaded component detection config file from {ConfigFile}", configFile);
}

/// <summary>
/// Merges two component detection configs, giving precedence to values already set in the first file.
/// </summary>
/// <param name="newConfig">The new config file to be merged into the existing config set.</param>
private void MergeComponentDetectionConfig(ComponentDetectionConfigFile newConfig)
{
foreach ((var name, var value) in newConfig.Variables)
{
if (!this.componentDetectionConfig.Variables.ContainsKey(name))
{
this.componentDetectionConfig.Variables[name] = value;
}
}
}

/// <summary>
/// Reads the component detection config from a file path.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public abstract class BaseSettings : CommandSettings
[CommandOption("--Output")]
public string Output { get; set; }

[Description("File path for a ComponentDetection.yml config file with more instructions for detection")]
[CommandOption("--ComponentDetectionConfigFile")]
public string ComponentDetectionConfigFile { get; set; }

/// <inheritdoc />
public override ValidationResult Validate()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public ScanCommand(
public override async Task<int> ExecuteAsync(CommandContext context, ScanSettings settings)
{
this.fileWritingService.Init(settings.Output);
await this.componentDetectionConfigFileService.InitAsync(settings.SourceDirectory.FullName);
await this.componentDetectionConfigFileService.InitAsync(settings.ComponentDetectionConfigFile, settings.SourceDirectory.FullName);
var result = await this.scanExecutionService.ExecuteScanAsync(settings);
this.WriteComponentManifest(settings, result);
return 0;
Expand Down

0 comments on commit 0c3e448

Please sign in to comment.