Skip to content

Commit

Permalink
feat: add option to pass arguments in environment variables
Browse files Browse the repository at this point in the history
varibles names: HISTORYCONTAINERURL, WRITESASTOKEN, REPOSITORYNAME
  • Loading branch information
LoremFooBar committed Jul 25, 2022
1 parent da3ae10 commit 624b8db
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class AzureBlobHistoryStorage : IHistoryStorage
public AzureBlobHistoryStorage()
{
var commandLineArguments = CommandLineArgumentsParser.GetCommandLineArguments();
var options = new CommandLineOptions(commandLineArguments);
var optionsFromEnvironment = EnvironmentVariablesOptionsReader.ReadOptionsFromEnvironment();
var options = new PluginOptions(optionsFromEnvironment.Any() ? optionsFromEnvironment : commandLineArguments);

_repositoryName = options.RepositoryName;
_gitRepositoryAccessor = new GitRepositoryAccessor();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace ReportGenerator.AzureBlobHistoryStorage;

public static class EnvironmentVariablesOptionsReader
{
public static Dictionary<string, string> ReadOptionsFromEnvironment()
{
string[] variables = { "HISTORYCONTAINERURL", "WRITESASTOKEN", "REPOSITORYNAME" };
var res = new Dictionary<string, string>();

foreach (string variable in variables) {
string? value = Environment.GetEnvironmentVariable(variable);

if (string.IsNullOrWhiteSpace(value)) continue;

res.Add(variable, value);
}

return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace ReportGenerator.AzureBlobHistoryStorage;

public class CommandLineOptions
public class PluginOptions
{
public CommandLineOptions(IReadOnlyDictionary<string, string> commandLineArguments)
public PluginOptions(IReadOnlyDictionary<string, string> commandLineArguments)
{
commandLineArguments.TryGetValue("DEBUG", out string? debug);
Debug = debug?.Equals("true", StringComparison.OrdinalIgnoreCase) == true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.13.0"/>
<PackageReference Include="CliWrap" Version="3.4.4"/>
<PackageReference Include="JetBrains.Annotations" Version="2022.1.0" PrivateAssets="All"/>
<PackageReference Include="ReportGenerator.Core" Version="5.1.9"/>
<PackageReference Include="Azure.Storage.Blobs" Version="12.13.0" />
<PackageReference Include="CliWrap" Version="3.4.4" />
<PackageReference Include="JetBrains.Annotations" Version="2022.1.0" PrivateAssets="All" />
<PackageReference Include="ReportGenerator.Core" Version="5.1.9" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ReportGenerator.AzureBlobHistoryStorage.Tests.CommandLineOptionsTests;

public class When_Calling_Constructor_With_All_Required_Arguments : SpecificationBase
{
private CommandLineOptions _commandLineOptions;
private PluginOptions _pluginOptions;

protected override void When()
{
Expand All @@ -18,15 +18,15 @@ protected override void When()
{ "repositoryname", "value3" },
};

_commandLineOptions = new CommandLineOptions(arguments);
_pluginOptions = new PluginOptions(arguments);
}

[Then]
public void It_Should_Create_CommandLineOptions_Instance()
{
_commandLineOptions.Should().NotBeNull();
_commandLineOptions.HistoryContainerUrl.Should().Be("value1");
_commandLineOptions.WriteSasToken.Should().Be("value2");
_commandLineOptions.RepositoryName.Should().Be("value3");
_pluginOptions.Should().NotBeNull();
_pluginOptions.HistoryContainerUrl.Should().Be("value1");
_pluginOptions.WriteSasToken.Should().Be("value2");
_pluginOptions.RepositoryName.Should().Be("value3");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ protected override void When()
base.When();

try {
_ = new CommandLineOptions(new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase));
_ = new PluginOptions(new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase));
}
catch (CommandLineArgumentMissingException ex1) {
_exception1 = ex1;
}

try {
_ = new CommandLineOptions(new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
_ = new PluginOptions(new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "writesastoken", "value2" },
{ "repositoryname", "value3" },
Expand All @@ -33,7 +33,7 @@ protected override void When()
}

try {
_ = new CommandLineOptions(new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
_ = new PluginOptions(new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "historycontainerurl", "value1" },
{ "repositoryname", "value3" },
Expand All @@ -44,7 +44,7 @@ protected override void When()
}

try {
_ = new CommandLineOptions(new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
_ = new PluginOptions(new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "historycontainerurl", "value1" },
{ "writesastoken", "value2" },
Expand Down

0 comments on commit 624b8db

Please sign in to comment.