From 624b8dbb685a2c3e461c8b09c822515cfc2737ca Mon Sep 17 00:00:00 2001 From: LoremFooBar Date: Sun, 24 Jul 2022 20:22:34 -0400 Subject: [PATCH] feat: add option to pass arguments in environment variables varibles names: HISTORYCONTAINERURL, WRITESASTOKEN, REPOSITORYNAME --- .../AzureBlobHistoryStorage.cs | 3 ++- .../EnvironmentVariablesOptionsReader.cs | 20 +++++++++++++++++++ ...CommandLineOptions.cs => PluginOptions.cs} | 4 ++-- ...rtGenerator.AzureBlobHistoryStorage.csproj | 8 ++++---- ...Constructor_With_All_Required_Arguments.cs | 12 +++++------ ...tructor_With_Required_Arguments_Missing.cs | 8 ++++---- 6 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 src/ReportGenerator.AzureBlobHistoryStorage/EnvironmentVariablesOptionsReader.cs rename src/ReportGenerator.AzureBlobHistoryStorage/{CommandLineOptions.cs => PluginOptions.cs} (92%) diff --git a/src/ReportGenerator.AzureBlobHistoryStorage/AzureBlobHistoryStorage.cs b/src/ReportGenerator.AzureBlobHistoryStorage/AzureBlobHistoryStorage.cs index bcb7d61..99b89b0 100644 --- a/src/ReportGenerator.AzureBlobHistoryStorage/AzureBlobHistoryStorage.cs +++ b/src/ReportGenerator.AzureBlobHistoryStorage/AzureBlobHistoryStorage.cs @@ -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(); diff --git a/src/ReportGenerator.AzureBlobHistoryStorage/EnvironmentVariablesOptionsReader.cs b/src/ReportGenerator.AzureBlobHistoryStorage/EnvironmentVariablesOptionsReader.cs new file mode 100644 index 0000000..67e72e9 --- /dev/null +++ b/src/ReportGenerator.AzureBlobHistoryStorage/EnvironmentVariablesOptionsReader.cs @@ -0,0 +1,20 @@ +namespace ReportGenerator.AzureBlobHistoryStorage; + +public static class EnvironmentVariablesOptionsReader +{ + public static Dictionary ReadOptionsFromEnvironment() + { + string[] variables = { "HISTORYCONTAINERURL", "WRITESASTOKEN", "REPOSITORYNAME" }; + var res = new Dictionary(); + + foreach (string variable in variables) { + string? value = Environment.GetEnvironmentVariable(variable); + + if (string.IsNullOrWhiteSpace(value)) continue; + + res.Add(variable, value); + } + + return res; + } +} diff --git a/src/ReportGenerator.AzureBlobHistoryStorage/CommandLineOptions.cs b/src/ReportGenerator.AzureBlobHistoryStorage/PluginOptions.cs similarity index 92% rename from src/ReportGenerator.AzureBlobHistoryStorage/CommandLineOptions.cs rename to src/ReportGenerator.AzureBlobHistoryStorage/PluginOptions.cs index f50ef1d..c8b9323 100644 --- a/src/ReportGenerator.AzureBlobHistoryStorage/CommandLineOptions.cs +++ b/src/ReportGenerator.AzureBlobHistoryStorage/PluginOptions.cs @@ -2,9 +2,9 @@ namespace ReportGenerator.AzureBlobHistoryStorage; -public class CommandLineOptions +public class PluginOptions { - public CommandLineOptions(IReadOnlyDictionary commandLineArguments) + public PluginOptions(IReadOnlyDictionary commandLineArguments) { commandLineArguments.TryGetValue("DEBUG", out string? debug); Debug = debug?.Equals("true", StringComparison.OrdinalIgnoreCase) == true; diff --git a/src/ReportGenerator.AzureBlobHistoryStorage/ReportGenerator.AzureBlobHistoryStorage.csproj b/src/ReportGenerator.AzureBlobHistoryStorage/ReportGenerator.AzureBlobHistoryStorage.csproj index acfe225..69ca927 100644 --- a/src/ReportGenerator.AzureBlobHistoryStorage/ReportGenerator.AzureBlobHistoryStorage.csproj +++ b/src/ReportGenerator.AzureBlobHistoryStorage/ReportGenerator.AzureBlobHistoryStorage.csproj @@ -11,10 +11,10 @@ - - - - + + + + diff --git a/tests/ReportGenerator.AzureBlobHistoryStorage.Tests/CommandLineOptionsTests/When_Calling_Constructor_With_All_Required_Arguments.cs b/tests/ReportGenerator.AzureBlobHistoryStorage.Tests/CommandLineOptionsTests/When_Calling_Constructor_With_All_Required_Arguments.cs index 4e97182..d5c3ed6 100644 --- a/tests/ReportGenerator.AzureBlobHistoryStorage.Tests/CommandLineOptionsTests/When_Calling_Constructor_With_All_Required_Arguments.cs +++ b/tests/ReportGenerator.AzureBlobHistoryStorage.Tests/CommandLineOptionsTests/When_Calling_Constructor_With_All_Required_Arguments.cs @@ -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() { @@ -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"); } } diff --git a/tests/ReportGenerator.AzureBlobHistoryStorage.Tests/CommandLineOptionsTests/When_Calling_Constructor_With_Required_Arguments_Missing.cs b/tests/ReportGenerator.AzureBlobHistoryStorage.Tests/CommandLineOptionsTests/When_Calling_Constructor_With_Required_Arguments_Missing.cs index 8ffc8e4..6a63e6f 100644 --- a/tests/ReportGenerator.AzureBlobHistoryStorage.Tests/CommandLineOptionsTests/When_Calling_Constructor_With_Required_Arguments_Missing.cs +++ b/tests/ReportGenerator.AzureBlobHistoryStorage.Tests/CommandLineOptionsTests/When_Calling_Constructor_With_Required_Arguments_Missing.cs @@ -15,14 +15,14 @@ protected override void When() base.When(); try { - _ = new CommandLineOptions(new Dictionary(StringComparer.OrdinalIgnoreCase)); + _ = new PluginOptions(new Dictionary(StringComparer.OrdinalIgnoreCase)); } catch (CommandLineArgumentMissingException ex1) { _exception1 = ex1; } try { - _ = new CommandLineOptions(new Dictionary(StringComparer.OrdinalIgnoreCase) + _ = new PluginOptions(new Dictionary(StringComparer.OrdinalIgnoreCase) { { "writesastoken", "value2" }, { "repositoryname", "value3" }, @@ -33,7 +33,7 @@ protected override void When() } try { - _ = new CommandLineOptions(new Dictionary(StringComparer.OrdinalIgnoreCase) + _ = new PluginOptions(new Dictionary(StringComparer.OrdinalIgnoreCase) { { "historycontainerurl", "value1" }, { "repositoryname", "value3" }, @@ -44,7 +44,7 @@ protected override void When() } try { - _ = new CommandLineOptions(new Dictionary(StringComparer.OrdinalIgnoreCase) + _ = new PluginOptions(new Dictionary(StringComparer.OrdinalIgnoreCase) { { "historycontainerurl", "value1" }, { "writesastoken", "value2" },