diff --git a/src/chocolatey.tests/infrastructure.app/commands/ChocolateySourceCommandSpecs.cs b/src/chocolatey.tests/infrastructure.app/commands/ChocolateySourceCommandSpecs.cs index aecaf9d987..55e6654e34 100644 --- a/src/chocolatey.tests/infrastructure.app/commands/ChocolateySourceCommandSpecs.cs +++ b/src/chocolatey.tests/infrastructure.app/commands/ChocolateySourceCommandSpecs.cs @@ -355,7 +355,7 @@ public void should_call_service_source_add_when_command_is_add() because(); configSettingsService.Verify(c => c.source_add(configuration), Times.Once); } - + [Fact] public void should_call_service_source_remove_when_command_is_remove() { @@ -380,5 +380,23 @@ public void should_call_service_source_enable_when_command_is_enable() configSettingsService.Verify(c => c.source_enable(configuration), Times.Once); } } + + public class when_list_is_called : ChocolateySourceCommandSpecsBase + { + private Action because; + + public override void Because() + { + because = () => command.list(configuration); + } + + [Fact] + public void should_call_service_source_list_when_command_is_list() + { + configuration.SourceCommand.Command = SourceCommandType.list; + because(); + configSettingsService.Verify(c => c.source_list(configuration), Times.Once); + } + } } } \ No newline at end of file diff --git a/src/chocolatey/GetChocolatey.cs b/src/chocolatey/GetChocolatey.cs index e6487625e5..b7c82b4bec 100644 --- a/src/chocolatey/GetChocolatey.cs +++ b/src/chocolatey/GetChocolatey.cs @@ -213,7 +213,16 @@ private void extract_resources() AssemblyFileExtractor.extract_all_resources_to_relative_directory(_container.GetInstance(), Assembly.GetAssembly(typeof(ChocolateyResourcesAssembly)), ApplicationParameters.InstallLocation, folders, ApplicationParameters.ChocolateyFileResources); } + + public IEnumerable List() + { + extract_resources(); + var configuration = create_configuration(new List()); + configuration.RegularOuptut = true; + var runner = new GenericRunner(); + return runner.list(configuration, _container, isConsole: false, parseArgs: null); + } } // ReSharper restore InconsistentNaming -} \ No newline at end of file +} diff --git a/src/chocolatey/chocolatey.csproj b/src/chocolatey/chocolatey.csproj index b008ab60a3..854bd24c55 100644 --- a/src/chocolatey/chocolatey.csproj +++ b/src/chocolatey/chocolatey.csproj @@ -90,6 +90,7 @@ + @@ -160,6 +161,7 @@ + diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateySourceCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateySourceCommand.cs index e62cafa7f8..537a24a7dc 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateySourceCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateySourceCommand.cs @@ -28,7 +28,7 @@ namespace chocolatey.infrastructure.app.commands [CommandFor(CommandNameType.sources)] [CommandFor(CommandNameType.source)] - public sealed class ChocolateySourceCommand : ICommand + public sealed class ChocolateySourceCommand : IListCommand { private readonly IChocolateyConfigSettingsService _configSettingsService; @@ -139,5 +139,10 @@ public void run(ChocolateyConfiguration configuration) break; } } + + public IEnumerable list(ChocolateyConfiguration configuration) + { + return _configSettingsService.source_list(configuration); + } } -} \ No newline at end of file +} diff --git a/src/chocolatey/infrastructure.app/configuration/ChocolateySource.cs b/src/chocolatey/infrastructure.app/configuration/ChocolateySource.cs new file mode 100644 index 0000000000..5c163e98c6 --- /dev/null +++ b/src/chocolatey/infrastructure.app/configuration/ChocolateySource.cs @@ -0,0 +1,25 @@ +// Copyright © 2015 - Present RealDimensions Software, LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace chocolatey.infrastructure.app.configuration +{ + public class ChocolateySource + { + public string Id { get; set; } + public string Value { get; set; } + public bool Disabled { get; set; } + public bool Authenticated { get; set; } + } +} diff --git a/src/chocolatey/infrastructure.app/runners/GenericRunner.cs b/src/chocolatey/infrastructure.app/runners/GenericRunner.cs index 99b14f1ace..f75e16ebbb 100644 --- a/src/chocolatey/infrastructure.app/runners/GenericRunner.cs +++ b/src/chocolatey/infrastructure.app/runners/GenericRunner.cs @@ -13,10 +13,12 @@ // See the License for the specific language governing permissions and // limitations under the License. + namespace chocolatey.infrastructure.app.runners { using System; using System.Linq; + using System.Collections.Generic; using SimpleInjector; using adapters; using attributes; @@ -28,7 +30,7 @@ namespace chocolatey.infrastructure.app.runners public sealed class GenericRunner { - public void run(ChocolateyConfiguration config, Container container, bool isConsole, Action parseArgs) + private ICommand find_command(ChocolateyConfiguration config, Container container, bool isConsole, Action parseArgs) { var commands = container.GetAllInstances(); var command = commands.Where((c) => @@ -83,7 +85,7 @@ Chocolatey is not an official build (bypassed with --allow-unofficial). If you are seeing this message and it is not expected, your system may now be in a bad state. Only official builds are to be trusted. " - ); + ); } } @@ -96,14 +98,38 @@ now be in a bad state. Only official builds are to be trusted. } command.noop(config); + return null; } - else + } + return command; + } + + public void run(ChocolateyConfiguration config, Container container, bool isConsole, Action parseArgs) + { + var command = find_command(config, container, isConsole, parseArgs); + if(command != null) + { + this.Log().Debug("_ {0}:{1} - Normal Run Mode _".format_with(ApplicationParameters.Name, command.GetType().Name)); + command.run(config); + } + } + + public IEnumerable list(ChocolateyConfiguration config, Container container, bool isConsole, Action parseArgs) + { + var command = find_command(config, container, isConsole, parseArgs) as IListCommand; + if (command == null) + { + if (!string.IsNullOrWhiteSpace(config.CommandName)) { - this.Log().Debug("_ {0}:{1} - Normal Run Mode _".format_with(ApplicationParameters.Name, command.GetType().Name)); - command.run(config); + throw new Exception("The implementation of '{0}' does not support listing '{1}'".format_with(config.CommandName, typeof(T).Name)); } + return new List(); + } + else + { + this.Log().Debug("_ {0}:{1} - Normal List Mode _".format_with(ApplicationParameters.Name, command.GetType().Name)); + return command.list(config); } } } - -} \ No newline at end of file +} diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyConfigSettingsService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyConfigSettingsService.cs index f45624b3e9..d1f4db70ca 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyConfigSettingsService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyConfigSettingsService.cs @@ -16,6 +16,7 @@ namespace chocolatey.infrastructure.app.services { using System; + using System.Collections.Generic; using System.Linq; using configuration; using infrastructure.services; @@ -43,12 +44,22 @@ public void noop(ChocolateyConfiguration configuration) this.Log().Info("Would have made a change to the configuration."); } - public void source_list(ChocolateyConfiguration configuration) + public IEnumerable source_list(ChocolateyConfiguration configuration) { + var list = new List(); foreach (var source in configFileSettings.Sources) { - this.Log().Info(() => "{0}{1} - {2}".format_with(source.Id, source.Disabled ? " [Disabled]" : string.Empty, source.Value)); - } + if (configuration.RegularOuptut) { + this.Log().Info(() => "{0}{1} - {2}".format_with(source.Id, source.Disabled ? " [Disabled]" : string.Empty, source.Value)); + } + list.Add(new ChocolateySource { + Id = source.Id, + Value = source.Value, + Disabled = source.Disabled, + Authenticated = string.IsNullOrWhiteSpace(source.Password) + }); + } + return list; } public void source_add(ChocolateyConfiguration configuration) @@ -232,4 +243,4 @@ public void set_api_key(ChocolateyConfiguration configuration) } } } -} \ No newline at end of file +} diff --git a/src/chocolatey/infrastructure.app/services/IChocolateyConfigSettingsService.cs b/src/chocolatey/infrastructure.app/services/IChocolateyConfigSettingsService.cs index 3d29562e6b..80628a4281 100644 --- a/src/chocolatey/infrastructure.app/services/IChocolateyConfigSettingsService.cs +++ b/src/chocolatey/infrastructure.app/services/IChocolateyConfigSettingsService.cs @@ -16,12 +16,13 @@ namespace chocolatey.infrastructure.app.services { using System; + using System.Collections.Generic; using configuration; public interface IChocolateyConfigSettingsService { void noop(ChocolateyConfiguration configuration); - void source_list(ChocolateyConfiguration configuration); + IEnumerable source_list(ChocolateyConfiguration configuration); void source_add(ChocolateyConfiguration configuration); void source_remove(ChocolateyConfiguration configuration); void source_disable(ChocolateyConfiguration configuration); @@ -32,4 +33,4 @@ public interface IChocolateyConfigSettingsService string get_api_key(ChocolateyConfiguration configuration, Action keyAction); void set_api_key(ChocolateyConfiguration configuration); } -} \ No newline at end of file +} diff --git a/src/chocolatey/infrastructure/commands/IListCommand.cs b/src/chocolatey/infrastructure/commands/IListCommand.cs new file mode 100644 index 0000000000..d24a89ccb2 --- /dev/null +++ b/src/chocolatey/infrastructure/commands/IListCommand.cs @@ -0,0 +1,25 @@ +// Copyright © 2015 - Present RealDimensions Software, LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace chocolatey.infrastructure.commands +{ + using System.Collections.Generic; + using app.configuration; + + public interface IListCommand : ICommand + { + IEnumerable list(ChocolateyConfiguration config); + } +}