forked from tomchavakis/nuget-license
-
-
Notifications
You must be signed in to change notification settings - Fork 31
feat: Add CSV output format support #484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // Licensed to the projects contributors. | ||
| // The license conditions are provided in the LICENSE file located in the project root | ||
|
|
||
| using System.Globalization; | ||
| using NuGetLicense.LicenseValidator; | ||
|
|
||
| namespace NuGetLicense.Output.Csv | ||
| { | ||
| /// <summary> | ||
| /// Outputs license validation results in CSV format. | ||
| /// </summary> | ||
| public class CsvOutputFormatter : IOutputFormatter | ||
| { | ||
| private readonly bool _printErrorsOnly; | ||
| private readonly bool _skipIgnoredPackages; | ||
|
|
||
| public CsvOutputFormatter(bool printErrorsOnly, bool skipIgnoredPackages) | ||
| { | ||
| _printErrorsOnly = printErrorsOnly; | ||
| _skipIgnoredPackages = skipIgnoredPackages; | ||
| } | ||
|
|
||
| public async Task Write(Stream stream, IList<LicenseValidationResult> results) | ||
| { | ||
| if (_printErrorsOnly) | ||
| { | ||
| results = results.Where(r => r.ValidationErrors.Any()).ToList(); | ||
| } | ||
| else if (_skipIgnoredPackages) | ||
| { | ||
| results = results.Where(r => r.LicenseInformationOrigin != LicenseInformationOrigin.Ignored).ToList(); | ||
| } | ||
|
|
||
| using var writer = new StreamWriter(stream, leaveOpen: true); | ||
|
|
||
| // Write header | ||
| await writer.WriteLineAsync("Package,Version,License Information Origin,License Expression,License Url,Copyright,Authors,Description,Summary,Error,Error Context"); | ||
|
|
||
| // Write rows | ||
| foreach (var result in results) | ||
| { | ||
| string errors = result.ValidationErrors.Any() | ||
| ? string.Join("; ", result.ValidationErrors.Select(e => EscapeCsvField(e.Error))) | ||
| : ""; | ||
| string errorContexts = result.ValidationErrors.Any() | ||
| ? string.Join("; ", result.ValidationErrors.Select(e => EscapeCsvField(e.Context))) | ||
| : ""; | ||
|
|
||
| var line = string.Join(",", | ||
| EscapeCsvField(result.PackageId), | ||
| EscapeCsvField(result.PackageVersion.ToString()), | ||
| EscapeCsvField(result.LicenseInformationOrigin.ToString()), | ||
| EscapeCsvField(result.License), | ||
| EscapeCsvField(result.LicenseUrl), | ||
| EscapeCsvField(result.Copyright), | ||
| EscapeCsvField(result.Authors), | ||
| EscapeCsvField(result.Description), | ||
| EscapeCsvField(result.Summary), | ||
| EscapeCsvField(errors), | ||
| EscapeCsvField(errorContexts) | ||
| ); | ||
|
|
||
| await writer.WriteLineAsync(line); | ||
| } | ||
|
|
||
| await writer.FlushAsync(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Escapes a field for CSV output. | ||
| /// Handles fields containing commas, quotes, or newlines. | ||
| /// </summary> | ||
| private static string EscapeCsvField(string? field) | ||
| { | ||
| if (string.IsNullOrEmpty(field)) | ||
| { | ||
| return ""; | ||
| } | ||
|
|
||
| // If the field contains comma, quote, or newline, wrap it in quotes | ||
| if (field.Contains(',') || field.Contains('"') || field.Contains('\n') || field.Contains('\r')) | ||
| { | ||
| // Replace double quotes with two double quotes | ||
| return "\"" + field.Replace("\"", "\"\"") + "\""; | ||
| } | ||
|
|
||
| return field; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid double-escaping
ErrorandError Contextvalues.Line 42–47 escapes each error item, and Line 59–60 escapes the joined field again. This can produce incorrect CSV cell content (extra quotes).
Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents