|
| 1 | +// This file is part of CycloneDX CLI Tool |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the “License”); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an “AS IS” BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// SPDX-License-Identifier: Apache-2.0 |
| 16 | +// Copyright (c) OWASP Foundation. All Rights Reserved. |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.Diagnostics.Contracts; |
| 20 | +using System.CommandLine; |
| 21 | +using System.CommandLine.Invocation; |
| 22 | +using System.Threading.Tasks; |
| 23 | +using CycloneDX.Models; |
| 24 | +using CycloneDX.Utils; |
| 25 | +using System.IO; |
| 26 | +using System.Collections.Immutable; |
| 27 | + |
| 28 | +namespace CycloneDX.Cli.Commands |
| 29 | +{ |
| 30 | + public static class RenameEntityCommand |
| 31 | + { |
| 32 | + public static void Configure(RootCommand rootCommand) |
| 33 | + { |
| 34 | + Contract.Requires(rootCommand != null); |
| 35 | + var subCommand = new System.CommandLine.Command("rename-entity", "Rename an entity identified by a \"bom-ref\" (including back-references to it) in the BOM document"); |
| 36 | + subCommand.Add(new Option<string>("--input-file", "Input BOM filename.")); |
| 37 | + subCommand.Add(new Option<string>("--output-file", "Output BOM filename, will write to stdout if no value provided.")); |
| 38 | + subCommand.Add(new Option<string>("--old-ref", "Old value of \"bom-ref\" entity identifier (or \"ref\" values or certain list items pointing to it).")); |
| 39 | + subCommand.Add(new Option<string>("--new-ref", "New value of \"bom-ref\" entity identifier (or \"ref\" values or certain list items pointing to it).")); |
| 40 | + subCommand.Add(new Option<CycloneDXBomFormat>("--input-format", "Specify input file format.")); |
| 41 | + subCommand.Add(new Option<CycloneDXBomFormat>("--output-format", "Specify output file format.")); |
| 42 | + subCommand.Handler = CommandHandler.Create<RenameEntityCommandOptions>(RenameEntity); |
| 43 | + rootCommand.Add(subCommand); |
| 44 | + } |
| 45 | + |
| 46 | + public static async Task<int> RenameEntity(RenameEntityCommandOptions options) |
| 47 | + { |
| 48 | + Contract.Requires(options != null); |
| 49 | + var outputToConsole = string.IsNullOrEmpty(options.OutputFile); |
| 50 | + |
| 51 | + if (options.OutputFormat == CycloneDXBomFormat.autodetect) |
| 52 | + { |
| 53 | + options.OutputFormat = CliUtils.AutoDetectBomFormat(options.OutputFile); |
| 54 | + if (options.OutputFormat == CycloneDXBomFormat.autodetect) |
| 55 | + { |
| 56 | + Console.WriteLine($"Unable to auto-detect output format"); |
| 57 | + return (int)ExitCode.ParameterValidationError; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + Console.WriteLine($"Loading input document..."); |
| 62 | + if (!outputToConsole) Console.WriteLine($"Processing input file {options.InputFile}"); |
| 63 | + var bom = await CliUtils.InputBomHelper(options.InputFile, options.InputFormat).ConfigureAwait(false); |
| 64 | + |
| 65 | + if (bom is null) |
| 66 | + { |
| 67 | + Console.WriteLine($"Empty or absent input document"); |
| 68 | + return (int)ExitCode.ParameterValidationError; |
| 69 | + } |
| 70 | + |
| 71 | + Console.WriteLine($"Beginning Bom walk to discover all identifiers (this can take a while)"); |
| 72 | + BomWalkResult bwr = bom.WalkThis(); |
| 73 | + |
| 74 | + Console.WriteLine($"Beginning Bom walk rename processing (this can take a while)"); |
| 75 | + if (bom.RenameBomRef(options.OldRef, options.NewRef, bwr)) |
| 76 | + { |
| 77 | + Console.WriteLine($"Did not encounter any issues during the rename operation"); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + Console.WriteLine($"Rename operation failed non-fatally (e.g. old ref name not mentioned in the Bom document)"); |
| 82 | + } |
| 83 | + |
| 84 | + // Ensure that the modified document has its own identity |
| 85 | + // (new SerialNumber, Version=1, Timestamp...) and its Tools |
| 86 | + // collection refers to this library and the program/tool |
| 87 | + // like cyclonedx-cli which consumes it: |
| 88 | + bom.BomMetadataUpdate(true); |
| 89 | + bom.BomMetadataReferThisToolkit(); |
| 90 | + |
| 91 | + if (!outputToConsole) |
| 92 | + { |
| 93 | + Console.WriteLine("Writing output file..."); |
| 94 | + Console.WriteLine($" Total {bom.Components?.Count ?? 0} components, {bom.Dependencies?.Count ?? 0} dependencies"); |
| 95 | + } |
| 96 | + |
| 97 | + int res = await CliUtils.OutputBomHelper(bom, options.OutputFormat, options.OutputFile).ConfigureAwait(false); |
| 98 | + return res; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments