Skip to content

Commit b6a8bcd

Browse files
authored
Merge pull request #20 from CycloneDX/previous-spec-versions
Add support for converting from previous specification versions
2 parents 34410ad + 24051c1 commit b6a8bcd

8 files changed

+96
-72
lines changed

cyclonedx/ConvertCommand.cs cyclonedx/Commands/Convert/ConvertCommand.cs

+11-46
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
using System.CommandLine;
33
using System.CommandLine.Invocation;
44
using System.IO;
5-
using System.Text;
65
using System.Threading.Tasks;
7-
using CycloneDX.Models;
6+
using CycloneDX.CLI.Commands;
7+
using CycloneDX.CLI.Models;
88

99
namespace CycloneDX.CLI
1010
{
@@ -15,39 +15,22 @@ internal static void ConfigureConvertCommand(RootCommand rootCommand)
1515
var subCommand = new Command("convert");
1616
subCommand.Add(new Option<string>("--input-file"));
1717
subCommand.Add(new Option<string>("--output-file"));
18-
subCommand.Add(new Option<ConvertInputFormat>("--input-format"));
18+
subCommand.Add(new Option<InputFormat>("--input-format"));
1919
subCommand.Add(new Option<ConvertOutputFormat>("--output-format"));
20-
subCommand.Handler = CommandHandler.Create<string, string, ConvertInputFormat, ConvertOutputFormat>(Convert);
20+
subCommand.Handler = CommandHandler.Create<string, string, InputFormat, ConvertOutputFormat>(Convert);
2121
rootCommand.Add(subCommand);
2222
}
2323

24-
public static async Task<int> Convert(string inputFile, string outputFile, ConvertInputFormat inputFormat, ConvertOutputFormat outputFormat)
24+
public static async Task<int> Convert(string inputFile, string outputFile, InputFormat inputFormat, ConvertOutputFormat outputFormat)
2525
{
26-
BomFormat inputBomFormat = BomFormat.Unsupported;
26+
var inputBomFormat = InputFormatHelper(inputFile, inputFormat);
27+
if (inputBomFormat == BomFormat.Unsupported) return (int)ExitCode.ParameterValidationError;
28+
2729
BomFormat outputBomFormat = BomFormat.Unsupported;
2830
string inputBomString;
29-
Bom inputBom;
31+
CycloneDX.Models.v1_2.Bom inputBom;
3032
string outputBomString;
3133

32-
if (inputFormat == ConvertInputFormat.autodetect)
33-
{
34-
if (string.IsNullOrEmpty(inputFile))
35-
{
36-
Console.Error.WriteLine("Unable to auto-detect input stream format, please specify a value for --input-format");
37-
return (int)ExitCode.ParameterValidationError;
38-
}
39-
inputBomFormat = Utils.DetectFileFormat(inputFile);
40-
if (inputBomFormat == BomFormat.Unsupported)
41-
{
42-
Console.Error.WriteLine("Unable to auto-detect input format from input filename");
43-
return (int)ExitCode.ParameterValidationError;
44-
}
45-
}
46-
else
47-
{
48-
inputBomFormat = (BomFormat)inputFormat;
49-
}
50-
5134
if (outputFormat == ConvertOutputFormat.autodetect)
5235
{
5336
if (string.IsNullOrEmpty(outputFile))
@@ -67,26 +50,8 @@ public static async Task<int> Convert(string inputFile, string outputFile, Conve
6750
outputBomFormat = (BomFormat)outputFormat;
6851
}
6952

70-
if (!string.IsNullOrEmpty(inputFile))
71-
{
72-
inputBomString = File.ReadAllText(inputFile);
73-
}
74-
else if (Console.IsInputRedirected)
75-
{
76-
var sb = new StringBuilder();
77-
string nextLine;
78-
do
79-
{
80-
nextLine = Console.ReadLine();
81-
sb.AppendLine(nextLine);
82-
} while (nextLine != null);
83-
inputBomString = sb.ToString();
84-
}
85-
else
86-
{
87-
Console.Error.WriteLine("You must specify a value for --input-file or pipe in an SBOM");
88-
return (int)ExitCode.ParameterValidationError;
89-
}
53+
inputBomString = InputFileHelper(inputFile);
54+
if (inputBomString == null) return (int)ExitCode.ParameterValidationError;
9055

9156
inputBom = Utils.BomDeserializer(inputBomString, inputBomFormat);
9257
outputBomString = Utils.BomSerializer(inputBom, outputBomFormat);

cyclonedx/ConvertOutputFormat.cs cyclonedx/Commands/Convert/ConvertOutputFormat.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace CycloneDX.CLI
1+
namespace CycloneDX.CLI.Commands
22
{
33
public enum ConvertOutputFormat
44
{

cyclonedx/ConvertInputFormat.cs

-9
This file was deleted.

cyclonedx/Models/InputFormat.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace CycloneDX.CLI.Models
2+
{
3+
public enum InputFormat
4+
{
5+
autodetect,
6+
xml,
7+
json
8+
}
9+
}

cyclonedx/Program.cs

+66-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,84 @@
11
using System;
22
using System.CommandLine;
3-
using System.CommandLine.Invocation;
3+
using System.IO;
4+
using System.Text;
45
using System.Threading.Tasks;
5-
using CycloneDX.Json;
6-
using CycloneDX.Models;
7-
using CycloneDX.Xml;
6+
using CycloneDX.CLI.Models;
87

98
namespace CycloneDX.CLI
109
{
1110
partial class Program
1211
{
12+
private static readonly string CycloneDX = @"
13+
______ __ ____ _ __ ________ ____
14+
/ ____/_ _______/ /___ ____ ___ / __ \ |/ / / ____/ / / _/
15+
/ / / / / / ___/ / __ \/ __ \/ _ \/ / / / / / / / / / /
16+
/ /___/ /_/ / /__/ / /_/ / / / / __/ /_/ / | / /___/ /____/ /
17+
\____/\__, /\___/_/\____/_/ /_/\___/_____/_/|_| \____/_____/___/
18+
/____/
19+
";
20+
1321
public static async Task<int> Main(string[] args)
1422
{
23+
if (args.Length == 0)
24+
{
25+
Console.WriteLine(CycloneDX);
26+
}
27+
1528
RootCommand rootCommand = new RootCommand();
1629

1730
ConfigureConvertCommand(rootCommand);
1831

1932
return await rootCommand.InvokeAsync(args);
2033
}
34+
35+
public static BomFormat InputFormatHelper(string inputFile, InputFormat inputFormat)
36+
{
37+
BomFormat inputBomFormat = BomFormat.Unsupported;
38+
39+
if (inputFormat == InputFormat.autodetect)
40+
{
41+
if (string.IsNullOrEmpty(inputFile))
42+
{
43+
Console.Error.WriteLine("Unable to auto-detect input stream format, please specify a value for --input-format");
44+
}
45+
inputBomFormat = Utils.DetectFileFormat(inputFile);
46+
if (inputBomFormat == BomFormat.Unsupported)
47+
{
48+
Console.Error.WriteLine("Unable to auto-detect input format from input filename");
49+
}
50+
}
51+
else
52+
{
53+
inputBomFormat = (BomFormat)inputFormat;
54+
}
55+
56+
return inputBomFormat;
57+
}
58+
59+
public static string InputFileHelper(string inputFile)
60+
{
61+
string inputString = null;
62+
if (!string.IsNullOrEmpty(inputFile))
63+
{
64+
inputString = File.ReadAllText(inputFile);
65+
}
66+
else if (Console.IsInputRedirected)
67+
{
68+
var sb = new StringBuilder();
69+
string nextLine;
70+
do
71+
{
72+
nextLine = Console.ReadLine();
73+
sb.AppendLine(nextLine);
74+
} while (nextLine != null);
75+
inputString = sb.ToString();
76+
}
77+
else
78+
{
79+
Console.Error.WriteLine("You must specify a value for --input-file or pipe in content");
80+
}
81+
return inputString;
82+
}
2183
}
2284
}

cyclonedx/SpdxTagSerializer.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Net;
44
using System.Text;
5-
using CycloneDX.Models;
65

76
namespace CycloneDX.CLI
87
{
@@ -13,12 +12,12 @@ public class SpdxSerializationException : Exception
1312
public SpdxSerializationException(string message) : base(message) {}
1413
}
1514

16-
public static string Serialize(Bom bom)
15+
public static string Serialize(CycloneDX.Models.v1_2.Bom bom)
1716
{
1817
if (bom.Metadata?.Component?.Name == null || bom.Metadata?.Component?.Version == null)
1918
throw new SpdxSerializationException("For SPDX output top level component name and version are required in the BOM metadata");
2019

21-
var nonSpdxLicenses = new List<License>();
20+
var nonSpdxLicenses = new List<CycloneDX.Models.v1_2.License>();
2221
string bomSpdxRef;
2322
if (string.IsNullOrEmpty(bom.SerialNumber))
2423
{
@@ -84,10 +83,10 @@ public static string Serialize(Bom bom)
8483
string algStr = null;
8584
switch (hash.Alg)
8685
{
87-
case Hash.HashAlgorithm.SHA_1:
86+
case CycloneDX.Models.v1_2.Hash.HashAlgorithm.SHA_1:
8887
algStr = "SHA1";
8988
break;
90-
case Hash.HashAlgorithm.SHA_256:
89+
case CycloneDX.Models.v1_2.Hash.HashAlgorithm.SHA_256:
9190
algStr = "SHA256";
9291
break;
9392
// following algorithms only supported in v2.2
@@ -157,7 +156,7 @@ public static string Serialize(Bom bom)
157156
sb.AppendLine();
158157
var license = nonSpdxLicenses[licenseIndex];
159158
sb.AppendLine($"LicenseID: LicenseRef-{licenseIndex+1}");
160-
sb.AppendLine($"ExtractedText: <text>\"{license.Name}\": {WebUtility.HtmlEncode(license.Text)}</text>");
159+
sb.AppendLine($"ExtractedText: <text>\"{license.Name}\": {WebUtility.HtmlEncode(license.Text.Content)}</text>");
161160
sb.AppendLine($"LicenseName: {license.Name}");
162161
if (!string.IsNullOrEmpty(license.Url))
163162
sb.AppendLine($"LicenseCrossReference: {license.Url}");

cyclonedx/Utils.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22
using System.IO;
3-
using System.Text;
4-
using CycloneDX.Models;
53
using CycloneDX.Json;
64
using CycloneDX.Xml;
75

@@ -26,7 +24,7 @@ public static BomFormat DetectFileFormat(string filename)
2624
}
2725
}
2826

29-
public static Bom BomDeserializer(string bom, BomFormat format)
27+
public static CycloneDX.Models.v1_2.Bom BomDeserializer(string bom, BomFormat format)
3028
{
3129
if (format == BomFormat.Json)
3230
{
@@ -39,7 +37,7 @@ public static Bom BomDeserializer(string bom, BomFormat format)
3937
throw new UnsupportedFormatException("Unsupported SBOM file format");
4038
}
4139

42-
public static string BomSerializer(Bom bom, BomFormat format)
40+
public static string BomSerializer(CycloneDX.Models.v1_2.Bom bom, BomFormat format)
4341
{
4442
if (format == BomFormat.Json)
4543
{

cyclonedx/cyclonedx.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
</PropertyGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="CycloneDX.Json" Version="0.2.2" />
19-
<PackageReference Include="CycloneDX.Xml" Version="0.2.2" />
18+
<PackageReference Include="CycloneDX.Json" Version="0.3.0" />
19+
<PackageReference Include="CycloneDX.Xml" Version="0.3.0" />
2020
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.1">
2121
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2222
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)