Skip to content

Commit

Permalink
(GH-302) Fix: choco pin list fails
Browse files Browse the repository at this point in the history
Sometimes when calling choco pin list, it fails to return a list of
valid items. This was due to "list" being added as a filter, so not all
locally installed items were being searched to see if they had a pin
for themselves.

Don't allow `config.Input` to be set or it will be passed along to list.
  • Loading branch information
ferventcoder committed Jun 5, 2015
1 parent f87d528 commit 21b5ea4
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/chocolatey/infrastructure.app/commands/ChocolateyPinCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public sealed class ChocolateyPinCommand : ICommand
private readonly IChocolateyPackageInformationService _packageInfoService;
private readonly ILogger _nugetLogger;
private readonly INugetService _nugetService;
private const string NO_CHANGE_MESSAGE = "Nothing to change. Pin already set or removed.";

public ChocolateyPinCommand(IChocolateyPackageInformationService packageInfoService, ILogger nugetLogger, INugetService nugetService)
{
Expand All @@ -56,7 +57,7 @@ public void configure_argument_parser(OptionSet optionSet, ChocolateyConfigurati

public void handle_additional_argument_parsing(IList<string> unparsedArguments, ChocolateyConfiguration configuration)
{
configuration.Input = string.Join(" ", unparsedArguments);
// don't set configuration.Input or it will be passed to list

if (unparsedArguments.Count > 1)
{
Expand All @@ -67,12 +68,12 @@ public void handle_additional_argument_parsing(IList<string> unparsedArguments,
string unparsedCommand = unparsedArguments.DefaultIfEmpty(string.Empty).FirstOrDefault();
Enum.TryParse(unparsedCommand, true, out command);

if (command == PinCommandType.unknown)
if (command == PinCommandType.unknown)
{
if (!string.IsNullOrWhiteSpace(unparsedCommand)) this.Log().Warn("Unknown command {0}. Setting to list.".format_with(unparsedCommand));
command = PinCommandType.list;
}

configuration.PinCommand.Command = command;
configuration.Sources = ApplicationParameters.PackagesLocation;
configuration.ListCommand.LocalOnly = true;
Expand Down Expand Up @@ -115,7 +116,7 @@ choco pin remove --name git

public void noop(ChocolateyConfiguration configuration)
{
this.Log().Info("Pin would have called {0} with other options:{1} Name={2}{1} Version={3}".format_with(configuration.PinCommand.Command.to_string(),Environment.NewLine,configuration.PinCommand.Name.to_string(),configuration.Version.to_string()));
this.Log().Info("Pin would have called {0} with other options:{1} Name={2}{1} Version={3}".format_with(configuration.PinCommand.Command.to_string(), Environment.NewLine, configuration.PinCommand.Name.to_string(), configuration.Version.to_string()));
}

public void run(ChocolateyConfiguration configuration)
Expand All @@ -124,7 +125,6 @@ public void run(ChocolateyConfiguration configuration)
installSuccessAction: null,
uninstallSuccessAction: null,
addUninstallHandler: false);

switch (configuration.PinCommand.Command)
{
case PinCommandType.list:
Expand All @@ -145,13 +145,15 @@ public void list_pins(IPackageManager packageManager, ChocolateyConfiguration co
var pkgInfo = _packageInfoService.get_package_information(pkg.Value.Package);
if (pkgInfo != null && pkgInfo.IsPinned)
{
this.Log().Info(() => "{0}|{1}".format_with(pkgInfo.Package.Id,pkgInfo.Package.Version));
this.Log().Info(() => "{0}|{1}".format_with(pkgInfo.Package.Id, pkgInfo.Package.Version));
}
}
}

public void set_pin(IPackageManager packageManager, ChocolateyConfiguration config)
{
var addingAPin = config.PinCommand.Command == PinCommandType.add;
this.Log().Info("Trying to {0} a pin for {1}".format_with(config.PinCommand.Command.to_string(), config.PinCommand.Name));
var versionUnspecified = string.IsNullOrWhiteSpace(config.Version);
SemanticVersion semanticVersion = versionUnspecified ? null : new SemanticVersion(config.Version);
IPackage installedPackage = packageManager.LocalRepository.FindPackage(config.PinCommand.Name, semanticVersion);
Expand All @@ -162,8 +164,19 @@ public void set_pin(IPackageManager packageManager, ChocolateyConfiguration conf

var pkgInfo = _packageInfoService.get_package_information(installedPackage);

pkgInfo.IsPinned = config.PinCommand.Command == PinCommandType.add;
bool changeMessage = pkgInfo.IsPinned != addingAPin;

pkgInfo.IsPinned = addingAPin;
_packageInfoService.save_package_information(pkgInfo);

if (changeMessage)
{
this.Log().Warn("Successfully {0} a pin for {1} v{2}.".format_with(addingAPin ? "added" : "removed", pkgInfo.Package.Id, pkgInfo.Package.Version.to_string()));
}
else
{
this.Log().Warn(NO_CHANGE_MESSAGE);
}
}

public bool may_require_admin_access()
Expand Down

0 comments on commit 21b5ea4

Please sign in to comment.