Skip to content

Commit

Permalink
MergeCommand: refactor DetermineInputFiles() into a separate method
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Jul 19, 2023
1 parent bbd41e4 commit 4017eb9
Showing 1 changed file with 55 additions and 49 deletions.
104 changes: 55 additions & 49 deletions src/cyclonedx/Commands/MergeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,60 @@ public static async Task<int> Merge(MergeCommandOptions options)
return (int)ExitCode.ParameterValidationError;
}

var inputBoms = await InputBoms(DetermineInputFiles(options), options.InputFormat, outputToConsole).ConfigureAwait(false);

Component bomSubject = null;
if (options.Group != null || options.Name != null || options.Version != null)
bomSubject = new Component
{
Type = Component.Classification.Application,
Group = options.Group,
Name = options.Name,
Version = options.Version,
};

Bom outputBom;
if (options.Hierarchical)
{
outputBom = CycloneDXUtils.HierarchicalMerge(inputBoms, bomSubject);
}
else
{
outputBom = CycloneDXUtils.FlatMerge(inputBoms);
if (outputBom.Metadata is null) outputBom.Metadata = new Metadata();
if (bomSubject != null)
{
// use the params provided if possible
outputBom.Metadata.Component = bomSubject;
}
else
{
// otherwise use the first non-null component from the input BOMs as the default
foreach (var bom in inputBoms)
{
if (bom.Metadata != null && bom.Metadata.Component != null)
{
outputBom.Metadata.Component = bom.Metadata.Component;
break;
}
}
}
}

outputBom.Version = 1;
outputBom.SerialNumber = "urn:uuid:" + System.Guid.NewGuid().ToString();

if (!outputToConsole)
{
Console.WriteLine("Writing output file...");
Console.WriteLine($" Total {outputBom.Components?.Count ?? 0} components");
}

return await CliUtils.OutputBomHelper(outputBom, options.OutputFormat, options.OutputFile).ConfigureAwait(false);
}

private static List<string> DetermineInputFiles(MergeCommandOptions options)
{
List<string> InputFiles;
if (options.InputFiles != null)
{
Expand Down Expand Up @@ -124,56 +178,8 @@ public static async Task<int> Merge(MergeCommandOptions options)
// in case the parameter was not passed
InputFiles = null;
}
var inputBoms = await InputBoms(InputFiles, options.InputFormat, outputToConsole).ConfigureAwait(false);

Component bomSubject = null;
if (options.Group != null || options.Name != null || options.Version != null)
bomSubject = new Component
{
Type = Component.Classification.Application,
Group = options.Group,
Name = options.Name,
Version = options.Version,
};

Bom outputBom;
if (options.Hierarchical)
{
outputBom = CycloneDXUtils.HierarchicalMerge(inputBoms, bomSubject);
}
else
{
outputBom = CycloneDXUtils.FlatMerge(inputBoms);
if (outputBom.Metadata is null) outputBom.Metadata = new Metadata();
if (bomSubject != null)
{
// use the params provided if possible
outputBom.Metadata.Component = bomSubject;
}
else
{
// otherwise use the first non-null component from the input BOMs as the default
foreach (var bom in inputBoms)
{
if(bom.Metadata != null && bom.Metadata.Component != null)
{
outputBom.Metadata.Component = bom.Metadata.Component;
break;
}
}
}
}

outputBom.Version = 1;
outputBom.SerialNumber = "urn:uuid:" + System.Guid.NewGuid().ToString();

if (!outputToConsole)
{
Console.WriteLine("Writing output file...");
Console.WriteLine($" Total {outputBom.Components?.Count ?? 0} components");
}

return await CliUtils.OutputBomHelper(outputBom, options.OutputFormat, options.OutputFile).ConfigureAwait(false);
return InputFiles;
}

private static async Task<IEnumerable<Bom>> InputBoms(IEnumerable<string> inputFilenames, CycloneDXBomFormat inputFormat, bool outputToConsole)
Expand Down

0 comments on commit 4017eb9

Please sign in to comment.