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 --format --verbosity and --configfile options to dotnet package search #5506

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace NuGet.CommandLine.XPlat
{
internal interface IPackageSearchResultPackage
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ internal class PackageSearchArgs
public bool Interactive { get; set; }
public ILoggerWithColor Logger { get; set; }
public string SearchTerm { get; set; }
public PackageSearchVerbosity Verbosity { get; set; } = PackageSearchVerbosity.Normal;
public bool JsonFormat { get; set; } = false;

public PackageSearchArgs(string skip, string take)
public PackageSearchArgs(string skip, string take, string format, string verbosity)
{
Skip = VerifyInt(skip, DefaultSkip);
Take = VerifyInt(take, DefaultTake);
Skip = VerifyInt(skip, DefaultSkip, "--skip");
Take = VerifyInt(take, DefaultTake, "--take");
JsonFormat = VerifyFormat(format);
Verbosity = VerifyVerbosity(verbosity);
}

public PackageSearchArgs() { }

public int VerifyInt(string number, int defaultValue)
public int VerifyInt(string number, int defaultValue, string option)
{
if (string.IsNullOrEmpty(number))
{
Expand All @@ -40,7 +44,53 @@ public int VerifyInt(string number, int defaultValue)
return verifiedNumber;
}

throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Error_invalid_number, number));
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Error_invalidOptionValue, number, option));
}

public bool VerifyFormat(string format)
{
if (!string.IsNullOrEmpty(format))
{
if (string.Equals(format, "json", StringComparison.CurrentCultureIgnoreCase))
{
return true;
}
else if (string.Equals(format, "table", StringComparison.CurrentCultureIgnoreCase))
{
return false;
}
else
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Error_invalidOptionValue, format, "--format"));
}
}

return false;
}

private PackageSearchVerbosity VerifyVerbosity(string verbosity)
{
if (verbosity != null)
{
if (string.Equals(verbosity, nameof(PackageSearchVerbosity.Detailed), StringComparison.CurrentCultureIgnoreCase))
{
return PackageSearchVerbosity.Detailed;
}
else if (string.Equals(verbosity, nameof(PackageSearchVerbosity.Normal), StringComparison.CurrentCultureIgnoreCase))
{
return PackageSearchVerbosity.Normal;
}
else if (string.Equals(verbosity, nameof(PackageSearchVerbosity.Minimal), StringComparison.CurrentCultureIgnoreCase))
{
return PackageSearchVerbosity.Minimal;
}
else
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Error_invalidOptionValue, verbosity, "--verbosity"));
}
}

return PackageSearchVerbosity.Normal;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void Register(CommandLineApplication app, Func<ILoggerWithColor> g
Register(app, getLogger, SetupSettingsAndRunSearchAsync);
}

public static void Register(CommandLineApplication app, Func<ILoggerWithColor> getLogger, Func<PackageSearchArgs, Task<int>> setupSettingsAndRunSearchAsync)
public static void Register(CommandLineApplication app, Func<ILoggerWithColor> getLogger, Func<PackageSearchArgs, string, Task<int>> setupSettingsAndRunSearchAsync)
{
app.Command("search", pkgSearch =>
{
Expand All @@ -35,6 +35,10 @@ public static void Register(CommandLineApplication app, Func<ILoggerWithColor> g
"--exact-match",
Strings.pkgSearch_ExactMatchDescription,
CommandOptionType.NoValue);
CommandOption format = pkgSearch.Option(
"--format",
Strings.pkgSearch_FormatDescription,
CommandOptionType.SingleValue);
CommandOption prerelease = pkgSearch.Option(
"--prerelease",
Strings.pkgSearch_PrereleaseDescription,
Expand All @@ -51,14 +55,22 @@ public static void Register(CommandLineApplication app, Func<ILoggerWithColor> g
"--skip",
Strings.pkgSearch_SkipDescription,
CommandOptionType.SingleValue);
CommandOption verbosity = pkgSearch.Option(
"--verbosity",
Strings.pkgSearch_VerbosityDescription,
CommandOptionType.SingleValue);
CommandOption configFile = pkgSearch.Option(
"--configfile",
Strings.pkgSearch_ConfigFileDescription,
CommandOptionType.SingleValue);

pkgSearch.OnExecute(async () =>
{
PackageSearchArgs packageSearchArgs;
ILoggerWithColor logger = getLogger();
try
{
packageSearchArgs = new PackageSearchArgs(skip.Value(), take.Value())
packageSearchArgs = new PackageSearchArgs(skip.Value(), take.Value(), format.Value(), verbosity.Value())
{
Sources = sources.Values,
SearchTerm = searchTerm.Value,
Expand All @@ -74,18 +86,18 @@ public static void Register(CommandLineApplication app, Func<ILoggerWithColor> g
return 1;
}

return await setupSettingsAndRunSearchAsync(packageSearchArgs);
return await setupSettingsAndRunSearchAsync(packageSearchArgs, configFile.Value());
});
});
}

public static async Task<int> SetupSettingsAndRunSearchAsync(PackageSearchArgs packageSearchArgs)
public static async Task<int> SetupSettingsAndRunSearchAsync(PackageSearchArgs packageSearchArgs, string configFile)
{
DefaultCredentialServiceUtility.SetupDefaultCredentialService(packageSearchArgs.Logger, !packageSearchArgs.Interactive);

ISettings settings = Settings.LoadDefaultSettings(
Directory.GetCurrentDirectory(),
configFileName: null,
configFileName: configFile,
machineWideSettings: new XPlatMachineWideSetting());
PackageSourceProvider sourceProvider = new PackageSourceProvider(settings);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using Newtonsoft.Json;

namespace NuGet.CommandLine.XPlat
{
/// <summary>
/// Represents the result of a package search for a specific source.
/// </summary>
internal class PackageSearchResult
{
[JsonProperty("sourceName")]
public string SourceName { get; set; }

[JsonProperty("packages")]
public List<IPackageSearchResultPackage> Packages { get; set; }

public PackageSearchResult(string source)
{
SourceName = source;
Packages = new List<IPackageSearchResultPackage>();
}

public void AddPackage(IPackageSearchResultPackage package)
{
Packages.Add(package);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using Newtonsoft.Json;
using NuGet.Configuration;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;

namespace NuGet.CommandLine.XPlat
{
internal class PackageSearchResultJsonPrinter : IPackageSearchResultRenderer
{
private ILoggerWithColor _logger;
private const int LineSeparatorLength = 40;
private static readonly string SourceSeparator = new('*', LineSeparatorLength);
private PackageSearchVerbosity _verbosity;
private List<PackageSearchResult> _packageSearchResults;

public PackageSearchResultJsonPrinter(ILoggerWithColor loggerWithColor, PackageSearchVerbosity verbosity)
{
_logger = loggerWithColor;
_verbosity = verbosity;
}
public async void Add(PackageSource source, IEnumerable<IPackageSearchMetadata> completedSearch)
{
PackageSearchResult packageSearchResult = new PackageSearchResult(source.Name);

foreach (IPackageSearchMetadata metadata in completedSearch)
{
if (_verbosity == PackageSearchVerbosity.Minimal)
{
packageSearchResult.AddPackage(new PackageSearchResultPackageMinimal(metadata));
}
else if (_verbosity == PackageSearchVerbosity.Detailed)
{
PackageDeprecationMetadata packageDeprecationMetadata = await metadata.GetDeprecationMetadataAsync();
packageSearchResult.AddPackage(new PackageSearchResultPackageDetailed(metadata, packageDeprecationMetadata?.Message));
}
else
{
packageSearchResult.AddPackage(new PackageSearchResultPackageNormal(metadata));
}
}

_packageSearchResults.Add(packageSearchResult);
}

public void Add(PackageSource source, string error)
{
_logger.LogMinimal(SourceSeparator);
_logger.LogMinimal($"Source: {source.Name} ({source.SourceUri})");
_logger.LogError(error);
}

public void Finish()
{
var json = JsonConvert.SerializeObject(_packageSearchResults, Formatting.Indented);
_logger.LogMinimal(SourceSeparator);
_logger.LogMinimal("Search Result");
_logger.LogMinimal(json);
}

public void Start()
{
_packageSearchResults = new List<PackageSearchResult>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using Newtonsoft.Json;
using NuGet.Protocol.Core.Types;

namespace NuGet.CommandLine.XPlat
{
internal class PackageSearchResultPackageDetailed : PackageSearchResultPackageNormal
{
[JsonProperty("description")]
public string Description { get; set; }

[JsonProperty("vulnerable")]
public bool? Vulnerable { get; set; }

[JsonProperty("deprecation")]
public string Deprecation { get; set; }

[JsonProperty("projectUrl")]
public Uri ProjectUrl { get; set; }

public PackageSearchResultPackageDetailed() : base()
{
}

public PackageSearchResultPackageDetailed(IPackageSearchMetadata packageSearchMetadata, string deprecation) : base(packageSearchMetadata)
{
Description = packageSearchMetadata.Description;

if (packageSearchMetadata.Vulnerabilities != null && packageSearchMetadata.Vulnerabilities.Any())
{
Vulnerable = true;
}

Deprecation = deprecation;
ProjectUrl = packageSearchMetadata.ProjectUrl;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Newtonsoft.Json;
using NuGet.Protocol.Core.Types;

namespace NuGet.CommandLine.XPlat
{
internal class PackageSearchResultPackageMinimal : IPackageSearchResultPackage
{
[JsonProperty("packageId")]
public string PackageId { get; set; }

[JsonProperty("latestVersion")]
public string LatestVersion { get; set; }

public PackageSearchResultPackageMinimal() { }

public PackageSearchResultPackageMinimal(IPackageSearchMetadata packageSearchMetadata)
{
PackageId = packageSearchMetadata.Identity.Id;
LatestVersion = packageSearchMetadata.Identity.Version.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Newtonsoft.Json;
using NuGet.Protocol.Core.Types;

namespace NuGet.CommandLine.XPlat
{
internal class PackageSearchResultPackageNormal : PackageSearchResultPackageMinimal
{
[JsonProperty("downloads")]
public long? Downloads { get; set; }

[JsonProperty("authors")]
public string Authors { get; set; }

public PackageSearchResultPackageNormal() : base() { }

public PackageSearchResultPackageNormal(IPackageSearchMetadata packageSearchMetadata) : base(packageSearchMetadata)
{
Downloads = packageSearchMetadata.DownloadCount;
Authors = packageSearchMetadata.Authors;
}
}
}
Loading