Skip to content

Commit

Permalink
Merge pull request #137 from Jaykul/master
Browse files Browse the repository at this point in the history
Start exposing lists and package results via the API #132
  • Loading branch information
ferventcoder committed Mar 5, 2015
2 parents 37716fa + 80a84e4 commit 1d8448c
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 17 deletions.
Expand Up @@ -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()
{
Expand All @@ -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);
}
}
}
}
11 changes: 10 additions & 1 deletion src/chocolatey/GetChocolatey.cs
Expand Up @@ -213,7 +213,16 @@ private void extract_resources()

AssemblyFileExtractor.extract_all_resources_to_relative_directory(_container.GetInstance<IFileSystem>(), Assembly.GetAssembly(typeof(ChocolateyResourcesAssembly)), ApplicationParameters.InstallLocation, folders, ApplicationParameters.ChocolateyFileResources);
}

public IEnumerable<T> List<T>()
{
extract_resources();
var configuration = create_configuration(new List<string>());
configuration.RegularOuptut = true;
var runner = new GenericRunner();
return runner.list<T>(configuration, _container, isConsole: false, parseArgs: null);
}
}

// ReSharper restore InconsistentNaming
}
}
2 changes: 2 additions & 0 deletions src/chocolatey/chocolatey.csproj
Expand Up @@ -90,6 +90,7 @@
<Compile Include="infrastructure.app\commands\ChocolateyUpdateCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyUpgradeCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyVersionCommand.cs" />
<Compile Include="infrastructure.app\configuration\ChocolateySource.cs" />
<Compile Include="infrastructure.app\configuration\ConfigFileFeatureSetting.cs" />
<Compile Include="infrastructure.app\configuration\PackagesConfigFilePackageSetting.cs" />
<Compile Include="infrastructure.app\configuration\PackagesConfigFileSettings.cs" />
Expand Down Expand Up @@ -160,6 +161,7 @@
<Compile Include="infrastructure\commandline\ExitScenarioHandler.cs" />
<Compile Include="infrastructure\commandline\InteractivePrompt.cs" />
<Compile Include="infrastructure\commands\ICommandExecutor.cs" />
<Compile Include="infrastructure\commands\IListCommand.cs" />
<Compile Include="infrastructure\commands\PowershellExecutor.cs" />
<Compile Include="infrastructure\guards\Ensure.cs" />
<Compile Include="infrastructure\information\ProcessInformation.cs" />
Expand Down
Expand Up @@ -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<ChocolateySource>
{
private readonly IChocolateyConfigSettingsService _configSettingsService;

Expand Down Expand Up @@ -139,5 +139,10 @@ public void run(ChocolateyConfiguration configuration)
break;
}
}

public IEnumerable<ChocolateySource> list(ChocolateyConfiguration configuration)
{
return _configSettingsService.source_list(configuration);
}
}
}
}
@@ -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; }
}
}
40 changes: 33 additions & 7 deletions src/chocolatey/infrastructure.app/runners/GenericRunner.cs
Expand Up @@ -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;
Expand All @@ -28,7 +30,7 @@ namespace chocolatey.infrastructure.app.runners

public sealed class GenericRunner
{
public void run(ChocolateyConfiguration config, Container container, bool isConsole, Action<ICommand> parseArgs)
private ICommand find_command(ChocolateyConfiguration config, Container container, bool isConsole, Action<ICommand> parseArgs)
{
var commands = container.GetAllInstances<ICommand>();
var command = commands.Where((c) =>
Expand Down Expand Up @@ -83,7 +85,7 @@ public void run(ChocolateyConfiguration config, Container container, bool isCons
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.
"
);
);

}
}
Expand All @@ -96,14 +98,38 @@ public void run(ChocolateyConfiguration config, Container container, bool isCons
}

command.noop(config);
return null;
}
else
}
return command;
}

public void run(ChocolateyConfiguration config, Container container, bool isConsole, Action<ICommand> 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<T> list<T>(ChocolateyConfiguration config, Container container, bool isConsole, Action<ICommand> parseArgs)
{
var command = find_command(config, container, isConsole, parseArgs) as IListCommand<T>;
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<T>();
}
else
{
this.Log().Debug("_ {0}:{1} - Normal List Mode _".format_with(ApplicationParameters.Name, command.GetType().Name));
return command.list(config);
}
}
}

}
}
Expand Up @@ -16,6 +16,7 @@
namespace chocolatey.infrastructure.app.services
{
using System;
using System.Collections.Generic;
using System.Linq;
using configuration;
using infrastructure.services;
Expand Down Expand Up @@ -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<ChocolateySource> source_list(ChocolateyConfiguration configuration)
{
var list = new List<ChocolateySource>();
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)
Expand Down Expand Up @@ -232,4 +243,4 @@ public void set_api_key(ChocolateyConfiguration configuration)
}
}
}
}
}
Expand Up @@ -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<ChocolateySource> source_list(ChocolateyConfiguration configuration);
void source_add(ChocolateyConfiguration configuration);
void source_remove(ChocolateyConfiguration configuration);
void source_disable(ChocolateyConfiguration configuration);
Expand All @@ -32,4 +33,4 @@ public interface IChocolateyConfigSettingsService
string get_api_key(ChocolateyConfiguration configuration, Action<ConfigFileApiKeySetting> keyAction);
void set_api_key(ChocolateyConfiguration configuration);
}
}
}
25 changes: 25 additions & 0 deletions 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<out T> : ICommand
{
IEnumerable<T> list(ChocolateyConfiguration config);
}
}

0 comments on commit 1d8448c

Please sign in to comment.