-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathConvertCommand.cs
More file actions
68 lines (62 loc) · 3.33 KB
/
Copy pathConvertCommand.cs
File metadata and controls
68 lines (62 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// This file is part of CycloneDX CLI Tool
//
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an “AS IS” BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) OWASP Foundation. All Rights Reserved.
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.NamingConventionBinder;
using System.Diagnostics.Contracts;
using System.Threading.Tasks;
using CycloneDX.Cli;
namespace CycloneDX.Cli.Commands
{
internal static class ConvertCommand
{
internal static void Configure(RootCommand rootCommand)
{
Contract.Requires(rootCommand != null);
var subCommand = new Command("convert", "Convert between different BOM formats");
subCommand.Add(new Option<string>("--input-file", "Input BOM filename, will read from stdin if no value provided."));
subCommand.Add(new Option<string>("--output-file", "Output BOM filename, will write to stdout if no value provided."));
subCommand.Add(new Option<ConvertFormat>("--input-format", "Specify input file format."));
subCommand.Add(new Option<ConvertFormat>("--output-format", "Specify output file format."));
subCommand.Add(new Option<SpecificationVersion>("--output-version", "Specify output BOM specification version. (ignored for CSV and SPDX formats)"));
subCommand.Handler = CommandHandler.Create<ConvertCommandOptions>(Convert);
rootCommand.Add(subCommand);
}
public static async Task<int> Convert(ConvertCommandOptions options)
{
Contract.Requires(options != null);
var inputBom = await CliUtils.InputBomHelper(options.InputFile, options.InputFormat).ConfigureAwait(false);
if (inputBom == null) return (int)ExitCode.ParameterValidationError;
if (options.OutputFormat == ConvertFormat.autodetect)
{
if (string.IsNullOrEmpty(options.OutputFile))
{
await Console.Error.WriteLineAsync("You must specify a value for --output-format when standard output is used").ConfigureAwait(false);
return (int)ExitCode.ParameterValidationError;
}
options.OutputFormat = CliUtils.AutoDetectConvertBomFormat(options.OutputFile);
if (options.OutputFormat == ConvertFormat.autodetect)
{
await Console.Error.WriteLineAsync("Unable to auto-detect output format from output filename").ConfigureAwait(false);
return (int)ExitCode.ParameterValidationError;
}
}
return await CliUtils.OutputBomHelper(inputBom, options.OutputFormat, options.OutputVersion, options.OutputFile).ConfigureAwait(false);
}
}
}