From 8a689bf95bed80840838a9ea15a289ad85b089bf Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Tue, 22 Mar 2016 16:04:52 -0500 Subject: [PATCH] (GH-663) Search by Id Starts With As an additional enhancement to searching by id, allow searching where the id starts with the filter. This adds to tab completion. The filters should be applied prior to searching by all versions gets returned, so move the filters up. Ensure the search is case-insensitive if it is local. A remote search is case insensitive if the remote server supports it. --- .../commands/ChocolateyListCommand.cs | 7 +++- .../configuration/ChocolateyConfiguration.cs | 1 + .../infrastructure.app/nuget/NugetList.cs | 41 ++++++++++++------- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs index a1bca53762..c952e78fcd 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs @@ -81,9 +81,12 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon .Add("e|exact", "Exact - Only return packages with this exact name.", option => configuration.ListCommand.Exact = option != null) - .Add("id|idonly|id-only", - "ByIdOnly - Only return packages with the filter being part of the id.", + .Add("by-id-only", + "ByIdOnly - Only return packages where the id contains the search filter.", option => configuration.ListCommand.ByIdOnly = option != null) + .Add("id-starts-with", + "IdStartsWith - Only return packages where the id starts with the search filter.", + option => configuration.ListCommand.IdStartsWith = option != null) ; } diff --git a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs index f4e25a4508..491ba1f850 100644 --- a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs +++ b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs @@ -357,6 +357,7 @@ public ListCommandConfiguration() public int PageSize { get; set; } public bool Exact { get; set; } public bool ByIdOnly { get; set; } + public bool IdStartsWith { get; set; } } [Serializable] diff --git a/src/chocolatey/infrastructure.app/nuget/NugetList.cs b/src/chocolatey/infrastructure.app/nuget/NugetList.cs index 6c844f550b..4d7c128974 100644 --- a/src/chocolatey/infrastructure.app/nuget/NugetList.cs +++ b/src/chocolatey/infrastructure.app/nuget/NugetList.cs @@ -15,6 +15,7 @@ namespace chocolatey.infrastructure.app.nuget { + using System; using System.Collections.Generic; using System.Linq; @@ -57,6 +58,31 @@ private static IQueryable execute_package_search(ChocolateyConfigurati IQueryable results = packageRepository.Search(configuration.Input, configuration.Prerelease); + + if (configuration.ListCommand.Page.HasValue) + { + results = results.Skip(configuration.ListCommand.PageSize * configuration.ListCommand.Page.Value).Take(configuration.ListCommand.PageSize); + } + + if (configuration.ListCommand.Exact) + { + results = results.Where(p => p.Id == configuration.Input); + } + + if (configuration.ListCommand.ByIdOnly) + { + results = isRemote ? + results.Where(p => p.Id.Contains(configuration.Input)) + : results.Where(p => p.Id.contains(configuration.Input, StringComparison.OrdinalIgnoreCase)); + } + + if (configuration.ListCommand.IdStartsWith) + { + results = isRemote ? + results.Where(p => p.Id.StartsWith(configuration.Input)) + : results.Where(p => p.Id.StartsWith(configuration.Input, StringComparison.OrdinalIgnoreCase)); + } + if (configuration.AllVersions) { if (isRemote) @@ -88,21 +114,6 @@ private static IQueryable execute_package_search(ChocolateyConfigurati .AsQueryable(); } - if (configuration.ListCommand.Page.HasValue) - { - results = results.Skip(configuration.ListCommand.PageSize * configuration.ListCommand.Page.Value).Take(configuration.ListCommand.PageSize); - } - - if (configuration.ListCommand.Exact) - { - results = results.Where(p => p.Id == configuration.Input); - } - - if (configuration.ListCommand.ByIdOnly) - { - results = results.Where(p => p.Id.Contains(configuration.Input)); - } - return results.OrderBy(p => p.Id); }