Skip to content
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

Add an ignored columns option #103

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Usage: nuget-license [options]
| `-include-ignored\|--include-ignored-packages` | If this option is set, the packages that are ignored from validation are still included in the output. |
| `-exclude-projects\|--exclude-projects-matching <EXCLUDED_PROJECTS>` | This option allows to specify project name(s) to exclude from the analysis. This can be useful to exclude test projects from the analysis when supplying a solution file as input. Wildcard characters (*) are supported to specify ranges of ignored projects. The input can either be a file name containing a list of project names in json format or a plain string that is then used as a single entry. |
| `-f\|--target-framework <TARGET_FRAMEWORK>` | This option allows to select a Target framework moniker (https://learn.microsoft.com/en-us/dotnet/standard/frameworks) for which to analyze dependencies. |
| `-ignored-columns\|--ignored-columns-from-output <column names>` | This option allows to remove columns from the table output. |
| `-?\|-h\|--help` | Show help information. |

## Example tool commands
Expand Down
12 changes: 11 additions & 1 deletion src/NuGetUtility/Output/Table/TableOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ public class TableOutputFormatter : IOutputFormatter
{
private readonly bool _printErrorsOnly;
private readonly bool _skipIgnoredPackages;
private readonly string[]? _ignoredColumns;

public TableOutputFormatter(bool printErrorsOnly, bool skipIgnoredPackages)
public TableOutputFormatter(bool printErrorsOnly, bool skipIgnoredPackages, IEnumerable<string>? ignoredColumns = null)
{
_printErrorsOnly = printErrorsOnly;
_skipIgnoredPackages = skipIgnoredPackages;
_ignoredColumns = ignoredColumns?.ToArray();
}

public async Task Write(Stream stream, IList<LicenseValidationResult> results)
Expand Down Expand Up @@ -41,6 +43,14 @@ public async Task Write(Stream stream, IList<LicenseValidationResult> results)
}
}

if (_ignoredColumns is not null)
{
foreach (ColumnDefinition? definition in columnDefinitions)
{
definition.Enabled &= !_ignoredColumns.Contains(definition.Title);
}
}

if (_printErrorsOnly)
{
results = results.Where(r => r.ValidationErrors.Any()).ToList();
Expand Down
22 changes: 21 additions & 1 deletion src/NuGetUtility/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public class Program
Description = "This option allows to select a Target framework moniker (https://learn.microsoft.com/en-us/dotnet/standard/frameworks) for which to analyze dependencies.")]
public string? TargetFramework { get; } = null;

[Option(LongName = "ignored-columns-from-output",
ShortName = "ignored-columns",
Description = "This option allows to specify column name(s) to exclude from the output")]
public string? IgnoredColumns { get; } = null;

private static string GetVersion()
=> typeof(Program).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? string.Empty;

Expand Down Expand Up @@ -182,7 +187,7 @@ private IOutputFormatter GetOutputFormatter()
{
OutputType.Json => new JsonOutputFormatter(false, ReturnErrorsOnly, !IncludeIgnoredPackages),
OutputType.JsonPretty => new JsonOutputFormatter(true, ReturnErrorsOnly, !IncludeIgnoredPackages),
OutputType.Table => new TableOutputFormatter(ReturnErrorsOnly, !IncludeIgnoredPackages),
OutputType.Table => new TableOutputFormatter(ReturnErrorsOnly, !IncludeIgnoredPackages, GetIgnoredColumns()),
_ => throw new ArgumentOutOfRangeException($"{OutputType} not supported")
};
}
Expand Down Expand Up @@ -286,5 +291,20 @@ private string[] GetInputFiles()

throw new FileNotFoundException("Please provide an input file");
}

private string[] GetIgnoredColumns()
{
if (IgnoredColumns == null)
{
return Array.Empty<string>();
}

if (File.Exists(IgnoredColumns))
{
return JsonSerializer.Deserialize<string[]>(File.ReadAllText(IgnoredColumns))!;
}

return new[] { IgnoredColumns };
}
}
}