From 993ccccccadbdcfb8499975a6b0bae1cf0f6707c Mon Sep 17 00:00:00 2001 From: Michael Tsfoni <80639729+mtsfoni@users.noreply.github.com> Date: Mon, 1 Jul 2024 12:52:48 +0200 Subject: [PATCH] feat: Detect output format from output filename if provided - Implemented logic to detect the output format from the output filename extension. - If `-fn` is set but `--json` or `--bom-format` are not set, the format is determined based on the file extension. - Supported extensions: .xml, .json, .proto, .pb, .bin. - Explicit parameters `--json` or `--bom-format` take precedence if set. Signed-off-by: Michael Tsfoni <80639729+mtsfoni@users.noreply.github.com> --- CycloneDX/Runner.cs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/CycloneDX/Runner.cs b/CycloneDX/Runner.cs index 890278a2..f8153636 100644 --- a/CycloneDX/Runner.cs +++ b/CycloneDX/Runner.cs @@ -64,6 +64,8 @@ public Runner() : this(null, null, null, null, null, null, null, null) { } public async Task HandleCommandAsync(RunOptions options) { options.outputDirectory ??= fileSystem.Directory.GetCurrentDirectory(); + SetOutputFormatFromFilename(options); + string outputDirectory = options.outputDirectory; string SolutionOrProjectFile = options.SolutionOrProjectFile; string framework = options.framework; @@ -511,5 +513,29 @@ internal static void AddMetadataTool(Bom bom) } } + private void SetOutputFormatFromFilename(RunOptions options) + { + if (!string.IsNullOrEmpty(options.outputFilename)) + { + var extension = Path.GetExtension(options.outputFilename).ToLowerInvariant(); + switch (extension) + { + case ".xml": + options.json = false; + break; + case ".json": + options.json = true; + break; + case ".proto": + case ".pb": + case ".bin": + // Add handling for other future formats here + break; + default: + Console.Error.WriteLine($"Unsupported file extension '{extension}' for output filename"); + break; + } + } + } } }