Skip to content

Commit

Permalink
(#513) Re-use existing Config from container
Browse files Browse the repository at this point in the history
When calling the showconfig command, it was re-hydrating the
configuration file from disk, rather than using the existing
configuration from the IoC container.

This commit changes the associated methods to re-use the existing
Config object, rather than creating it again from scratch.  This has
the result of only showing the Warning about a missing Yaml file once,
and not twice.
  • Loading branch information
gep13 committed Aug 24, 2023
1 parent 500c81e commit 789b12d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Threading.Tasks;
using GitReleaseManager.Core.Commands;
using GitReleaseManager.Core.Configuration;
using GitReleaseManager.Core.Helpers;
using GitReleaseManager.Core.Options;
using NSubstitute;
Expand All @@ -21,7 +23,10 @@ public void Setup()
{
_fileSystem = Substitute.For<IFileSystem>();
_logger = Substitute.For<ILogger>();
_command = new ShowConfigCommand(_fileSystem, _logger);

var currentDirectory = Environment.CurrentDirectory;
var configuration = ConfigurationProvider.Provide(currentDirectory, _fileSystem);
_command = new ShowConfigCommand(_logger, configuration);
}

[Test]
Expand Down
10 changes: 4 additions & 6 deletions src/GitReleaseManager.Core/Commands/ShowConfigCommand.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
using System;
using System.Threading.Tasks;
using GitReleaseManager.Core.Configuration;
using GitReleaseManager.Core.Helpers;
using GitReleaseManager.Core.Options;
using Serilog;

namespace GitReleaseManager.Core.Commands
{
public class ShowConfigCommand : ICommand<ShowConfigSubOptions>
{
private readonly IFileSystem _fileSystem;
private readonly ILogger _logger;
private readonly Config _config;

public ShowConfigCommand(IFileSystem fileSystem, ILogger logger)
public ShowConfigCommand(ILogger logger, Config config)
{
_fileSystem = fileSystem;
_logger = logger;
_config = config;
}

public Task<int> ExecuteAsync(ShowConfigSubOptions options)
{
var configuration = ConfigurationProvider.GetEffectiveConfigAsString(options.TargetDirectory ?? Environment.CurrentDirectory, _fileSystem);
var configuration = ConfigurationProvider.GetEffectiveConfigAsString(_config);
_logger.Information("{Configuration}", configuration);

return Task.FromResult(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ public static Config Provide(string gitDirectory, IFileSystem fileSystem)
return new Config();
}

public static string GetEffectiveConfigAsString(string currentDirectory, IFileSystem fileSystem)
public static string GetEffectiveConfigAsString(Config config)
{
var config = Provide(currentDirectory, fileSystem);
var stringBuilder = new StringBuilder();
using (var stream = new StringWriter(stringBuilder, CultureInfo.InvariantCulture))
{
Expand Down

0 comments on commit 789b12d

Please sign in to comment.