Ejemplo n.º 1
0
        public static BomFormat InputFormatHelper(string inputFile, InputFormat inputFormat)
        {
            if (inputFormat == InputFormat.autodetect)
            {
                if (string.IsNullOrEmpty(inputFile))
                {
                    Console.Error.WriteLine("Unable to auto-detect input stream format, please specify a value for --input-format");
                }
                var inputBomFormat = CLIUtils.DetectFileFormat(inputFile);
                if (inputBomFormat == BomFormat.Unsupported)
                {
                    Console.Error.WriteLine("Unable to auto-detect input format from input filename");
                }
                return(inputBomFormat);
            }
            else
            {
                if (inputFormat == InputFormat.json)
                {
                    return(BomFormat.Json);
                }
                else if (inputFormat == InputFormat.xml)
                {
                    return(BomFormat.Xml);
                }
                else if (inputFormat == InputFormat.csv)
                {
                    return(BomFormat.Csv);
                }
            }

            return(BomFormat.Unsupported);
        }
Ejemplo n.º 2
0
        public static async Task <int> Convert(string inputFile, string outputFile, InputFormat inputFormat, ConvertOutputFormat outputFormat)
        {
            var inputBomFormat = InputFormatHelper(inputFile, inputFormat);

            if (inputBomFormat == BomFormat.Unsupported)
            {
                return((int)ExitCode.ParameterValidationError);
            }

            BomFormat outputBomFormat = BomFormat.Unsupported;
            string    inputBomString;
            Bom       inputBom;
            string    outputBomString;

            if (outputFormat == ConvertOutputFormat.autodetect)
            {
                if (string.IsNullOrEmpty(outputFile))
                {
                    Console.Error.WriteLine("You must specify a value for --output-format when standard output is used");
                    return((int)ExitCode.ParameterValidationError);
                }
                outputBomFormat = CLIUtils.DetectFileFormat(outputFile);
                if (outputBomFormat == BomFormat.Unsupported)
                {
                    Console.Error.WriteLine("Unable to auto-detect output format from output filename");
                    return((int)ExitCode.ParameterValidationError);
                }
            }
            else
            {
                outputBomFormat = (BomFormat)outputFormat;
            }

            inputBomString = await InputFileHelper(inputFile);

            if (inputBomString == null)
            {
                return((int)ExitCode.ParameterValidationError);
            }

            inputBom        = CLIUtils.BomDeserializer(inputBomString, inputBomFormat);
            outputBomString = CLIUtils.BomSerializer(inputBom, outputBomFormat);

            if (string.IsNullOrEmpty(outputFile))
            {
                Console.Write(outputBomString);
            }
            else
            {
                Console.WriteLine("Writing output file...");
                await File.WriteAllTextAsync(outputFile, outputBomString);
            }

            return((int)ExitCode.Ok);
        }
Ejemplo n.º 3
0
        public static async Task <int> Diff(
            string fromFile, string toFile, InputFormat fromFormat, InputFormat toFormat, StandardOutputFormat outputFormat,
            bool componentVersions)
        {
            var fromBomFormat = InputFormatHelper(fromFile, fromFormat);
            var toBomFormat   = InputFormatHelper(toFile, toFormat);

            if (fromBomFormat == BomFormat.Unsupported || toBomFormat == BomFormat.Unsupported)
            {
                return((int)ExitCode.ParameterValidationError);
            }

            var fromBomString = await File.ReadAllTextAsync(fromFile);

            var toBomString = await File.ReadAllTextAsync(toFile);

            var fromBom = CLIUtils.BomDeserializer(fromBomString, fromBomFormat);
            var toBom   = CLIUtils.BomDeserializer(toBomString, toBomFormat);

            var result = new DiffResult();

            if (componentVersions)
            {
                result.ComponentVersions = CycloneDXUtils.ComponentVersionDiff(fromBom, toBom);
            }

            if (outputFormat == StandardOutputFormat.json)
            {
                var options = new JsonSerializerOptions
                {
                    WriteIndented        = true,
                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                    IgnoreNullValues     = true,
                };

                options.Converters.Add(new Json.Converters.v1_2.ComponentTypeConverter());
                options.Converters.Add(new Json.Converters.v1_2.DataFlowConverter());
                options.Converters.Add(new Json.Converters.v1_2.DateTimeConverter());
                options.Converters.Add(new Json.Converters.v1_2.DependencyConverter());
                options.Converters.Add(new Json.Converters.v1_2.ExternalReferenceTypeConverter());
                options.Converters.Add(new Json.Converters.v1_2.HashAlgorithmConverter());
                options.Converters.Add(new Json.Converters.v1_2.IssueClassificationConverter());
                options.Converters.Add(new Json.Converters.v1_2.LicenseConverter());
                options.Converters.Add(new Json.Converters.v1_2.PatchClassificationConverter());

                Console.WriteLine(JsonSerializer.Serialize(result, options));
            }
            else
            {
                if (result.ComponentVersions != null)
                {
                    Console.WriteLine("Component versions that have changed:");
                    Console.WriteLine();

                    var changes = false;
                    foreach (var entry in result.ComponentVersions)
                    {
                        var componentDiffItem = entry.Value;
                        if (componentDiffItem.Added.Count > 0 || componentDiffItem.Removed.Count > 0)
                        {
                            changes = true;
                            foreach (var component in componentDiffItem.Removed)
                            {
                                Console.WriteLine($"- {component.Group} {component.Name} @ {component.Version}");
                            }
                            foreach (var component in componentDiffItem.Unchanged)
                            {
                                Console.WriteLine($"= {component.Group} {component.Name} @ {component.Version}");
                            }
                            foreach (var component in componentDiffItem.Added)
                            {
                                Console.WriteLine($"+ {component.Group} {component.Name} @ {component.Version}");
                            }
                            Console.WriteLine();
                        }
                    }

                    if (!changes)
                    {
                        Console.WriteLine("None");
                    }

                    Console.WriteLine();
                }
            }

            return((int)ExitCode.Ok);
        }
Ejemplo n.º 4
0
        public static async Task <int> Analyze(
            string inputFile, InputFormat inputFormat, StandardOutputFormat outputFormat,
            bool multipleComponentVersions)
        {
            var inputBomFormat = InputFormatHelper(inputFile, inputFormat);

            if (inputBomFormat == BomFormat.Unsupported)
            {
                return((int)ExitCode.ParameterValidationError);
            }

            var inputBomString = await InputFileHelper(inputFile);

            if (inputBomString == null)
            {
                return((int)ExitCode.ParameterValidationError);
            }

            var inputBom = CLIUtils.BomDeserializer(inputBomString, inputBomFormat);

            var result = new AnalyzeResult();

            if (multipleComponentVersions)
            {
                result.MultipleComponentVersions = CycloneDXUtils.MultipleComponentVersions(inputBom);
            }

            if (outputFormat == StandardOutputFormat.json)
            {
                var options = new JsonSerializerOptions
                {
                    WriteIndented        = true,
                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                    IgnoreNullValues     = true,
                };

                options.Converters.Add(new Json.Converters.v1_2.ComponentTypeConverter());
                options.Converters.Add(new Json.Converters.v1_2.DataFlowConverter());
                options.Converters.Add(new Json.Converters.v1_2.DateTimeConverter());
                options.Converters.Add(new Json.Converters.v1_2.DependencyConverter());
                options.Converters.Add(new Json.Converters.v1_2.ExternalReferenceTypeConverter());
                options.Converters.Add(new Json.Converters.v1_2.HashAlgorithmConverter());
                options.Converters.Add(new Json.Converters.v1_2.IssueClassificationConverter());
                options.Converters.Add(new Json.Converters.v1_2.LicenseConverter());
                options.Converters.Add(new Json.Converters.v1_2.PatchClassificationConverter());

                Console.WriteLine(JsonSerializer.Serialize(result, options));
            }
            else
            {
                if (inputBom.Metadata?.Component != null)
                {
                    var component = inputBom.Metadata.Component;
                    Console.WriteLine($"Analysis results for {component.Name}@{component.Version}:");
                }
                else
                {
                    Console.WriteLine("Analysis results:");
                }
                Console.WriteLine();

                if (result.MultipleComponentVersions != null)
                {
                    Console.WriteLine("Components with multiple versions:");
                    Console.WriteLine();
                    if (result.MultipleComponentVersions.Count == 0)
                    {
                        Console.WriteLine("None");
                    }
                    else
                    {
                        foreach (var componentEntry in result.MultipleComponentVersions)
                        {
                            Console.Write(componentEntry.Key);
                            Console.Write(" versions:");
                            foreach (var component in componentEntry.Value)
                            {
                                Console.Write(" ");
                                Console.Write(component.Version);
                            }
                            Console.WriteLine();
                        }
                    }
                    Console.WriteLine();
                }
            }

            return((int)ExitCode.Ok);
        }