2
2
using System . CommandLine ;
3
3
using System . CommandLine . Invocation ;
4
4
using System . IO ;
5
- using System . Text ;
6
5
using System . Threading . Tasks ;
7
- using CycloneDX . Models ;
6
+ using CycloneDX . CLI . Commands ;
7
+ using CycloneDX . CLI . Models ;
8
8
9
9
namespace CycloneDX . CLI
10
10
{
@@ -15,39 +15,22 @@ internal static void ConfigureConvertCommand(RootCommand rootCommand)
15
15
var subCommand = new Command ( "convert" ) ;
16
16
subCommand . Add ( new Option < string > ( "--input-file" ) ) ;
17
17
subCommand . Add ( new Option < string > ( "--output-file" ) ) ;
18
- subCommand . Add ( new Option < ConvertInputFormat > ( "--input-format" ) ) ;
18
+ subCommand . Add ( new Option < InputFormat > ( "--input-format" ) ) ;
19
19
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 ) ;
21
21
rootCommand . Add ( subCommand ) ;
22
22
}
23
23
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 )
25
25
{
26
- BomFormat inputBomFormat = BomFormat . Unsupported ;
26
+ var inputBomFormat = InputFormatHelper ( inputFile , inputFormat ) ;
27
+ if ( inputBomFormat == BomFormat . Unsupported ) return ( int ) ExitCode . ParameterValidationError ;
28
+
27
29
BomFormat outputBomFormat = BomFormat . Unsupported ;
28
30
string inputBomString ;
29
- Bom inputBom ;
31
+ CycloneDX . Models . v1_2 . Bom inputBom ;
30
32
string outputBomString ;
31
33
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
-
51
34
if ( outputFormat == ConvertOutputFormat . autodetect )
52
35
{
53
36
if ( string . IsNullOrEmpty ( outputFile ) )
@@ -67,26 +50,8 @@ public static async Task<int> Convert(string inputFile, string outputFile, Conve
67
50
outputBomFormat = ( BomFormat ) outputFormat ;
68
51
}
69
52
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 ;
90
55
91
56
inputBom = Utils . BomDeserializer ( inputBomString , inputBomFormat ) ;
92
57
outputBomString = Utils . BomSerializer ( inputBom , outputBomFormat ) ;
0 commit comments