Skip to content

Commit

Permalink
(GH-71) Sources can have explicit priority order
Browse files Browse the repository at this point in the history
When a priority order is specified, sources will order explicitly by
that order, lowest number above zero will have the highest priority and
so on, then any items with a priority of 0 will be evaluated in
configuration file order.
  • Loading branch information
ferventcoder committed Sep 21, 2015
1 parent 583867e commit 11d1c47
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
13 changes: 11 additions & 2 deletions src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ private static void set_file_configuration(ChocolateyConfiguration config, IFile

var configFileSettings = xmlService.deserialize<ConfigFileSettings>(globalConfigPath);
var sources = new StringBuilder();
foreach (var source in configFileSettings.Sources.Where(s => !s.Disabled).or_empty_list_if_null())

var defaultSourcesInOrder = configFileSettings.Sources.Where(s => !s.Disabled).or_empty_list_if_null().ToList();
if (configFileSettings.Sources.Any(s => s.Priority > 0))
{
defaultSourcesInOrder = configFileSettings.Sources.Where(s => !s.Disabled && s.Priority != 0).OrderBy(s => s.Priority).or_empty_list_if_null().ToList();
defaultSourcesInOrder.AddRange(configFileSettings.Sources.Where(s => !s.Disabled && s.Priority == 0 ).or_empty_list_if_null().ToList());
}

foreach (var source in defaultSourcesInOrder)
{
sources.AppendFormat("{0};", source.Value);
}
Expand Down Expand Up @@ -120,7 +128,8 @@ private static void set_machine_sources(ChocolateyConfiguration config, ConfigFi
Key = source.Value,
Name = source.Id,
Username = source.UserName,
EncryptedPassword = source.Password
EncryptedPassword = source.Password,
Priority = source.Priority
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public void configure_argument_parser(OptionSet optionSet, ChocolateyConfigurati
option => configuration.SourceCommand.Username = option.remove_surrounding_quotes())
.Add("p=|password=",
"Password - the user's password to the source. Encrypted in chocolatey.config file.",
option => configuration.SourceCommand.Password = option.remove_surrounding_quotes())
option => configuration.SourceCommand.Password = option.remove_surrounding_quotes())
.Add("priority=",
"Priority - The priority order of this source as compared to other sources, lower is better. Defaults to 0 (no priority). All priorities above 0 will be evaluated first, then zero-based values will be evaluated in config file order.",
option => configuration.SourceCommand.Priority = int.Parse(option.remove_surrounding_quotes()))
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ public sealed class SourcesCommandConfiguration
public SourceCommandType Command { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public int Priority { get; set; }
}

[Serializable]
Expand All @@ -364,6 +365,7 @@ public sealed class MachineSourceConfiguration
public string Key { get; set; }
public string Username { get; set; }
public string EncryptedPassword { get; set; }
public int Priority { get; set; }
}

[Serializable]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public sealed class ConfigFileSourceSetting
public string UserName { get; set; }

[XmlAttribute(AttributeName = "password")]
public string Password { get; set; }
public string Password { get; set; }

[XmlAttribute(AttributeName = "priority")]
public int Priority { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace chocolatey.infrastructure.app.services
using System.Linq;
using configuration;
using infrastructure.services;
using logging;
using nuget;

internal class ChocolateyConfigSettingsService : IChocolateyConfigSettingsService
Expand Down Expand Up @@ -47,7 +48,12 @@ public void source_list(ChocolateyConfiguration configuration)
{
foreach (var source in configFileSettings.Sources)
{
this.Log().Info(() => "{0}{1} - {2}".format_with(source.Id, source.Disabled ? " [Disabled]" : string.Empty, source.Value));
this.Log().Info(() => "{0}{1} - {2} {3}| Priority {4}.".format_with(
source.Id,
source.Disabled ? " [Disabled]" : string.Empty,
source.Value,
string.IsNullOrWhiteSpace(source.UserName) ? string.Empty : "(Authenticated)",
source.Priority));
}
}

Expand All @@ -56,17 +62,18 @@ public void source_add(ChocolateyConfiguration configuration)
var source = configFileSettings.Sources.FirstOrDefault(p => p.Id.is_equal_to(configuration.SourceCommand.Name));
if (source == null)
{
configFileSettings.Sources.Add(new ConfigFileSourceSetting
{
Id = configuration.SourceCommand.Name,
Value = configuration.Sources,
UserName = configuration.SourceCommand.Username,
Password = NugetEncryptionUtility.EncryptString(configuration.SourceCommand.Password),
});
source = new ConfigFileSourceSetting
{
Id = configuration.SourceCommand.Name,
Value = configuration.Sources,
UserName = configuration.SourceCommand.Username,
Password = NugetEncryptionUtility.EncryptString(configuration.SourceCommand.Password),
Priority = configuration.SourceCommand.Priority
};
configFileSettings.Sources.Add(source);

_xmlService.serialize(configFileSettings, ApplicationParameters.GlobalConfigFileLocation);

this.Log().Warn(() => "Added {0} - {1}".format_with(configuration.SourceCommand.Name, configuration.Sources));
this.Log().Warn(() => "Added {0} - {1} (Priority {2})".format_with(source.Id, source.Value, source.Priority));
}
else
{
Expand Down

0 comments on commit 11d1c47

Please sign in to comment.