From f16921d12d2e72a7e00d087967f46ddabe022d15 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Tue, 7 Sep 2021 15:56:59 +0200 Subject: [PATCH] Allow environment variables for Tapeti.Cmd connection parameters Output connection parameters and their sources to the console --- Tapeti.Cmd/Program.cs | 21 +++--- Tapeti.Cmd/Verbs/BaseConnectionOptions.cs | 85 +++++++++++++++++++++-- Tapeti.Cmd/Verbs/BindQueueVerb.cs | 10 +-- Tapeti.Cmd/Verbs/DeclareQueueVerb.cs | 10 +-- Tapeti.Cmd/Verbs/ExportVerb.cs | 10 +-- Tapeti.Cmd/Verbs/ImportVerb.cs | 11 +-- Tapeti.Cmd/Verbs/PurgeVerb.cs | 11 +-- Tapeti.Cmd/Verbs/RemoveQueueVerb.cs | 10 +-- Tapeti.Cmd/Verbs/ShovelVerb.cs | 12 +--- Tapeti.Cmd/Verbs/UnbindQueueVerb.cs | 10 +-- 10 files changed, 99 insertions(+), 91 deletions(-) diff --git a/Tapeti.Cmd/Program.cs b/Tapeti.Cmd/Program.cs index 787d326..abc3f85 100644 --- a/Tapeti.Cmd/Program.cs +++ b/Tapeti.Cmd/Program.cs @@ -17,6 +17,9 @@ namespace Tapeti.Cmd .Where(t => t.GetCustomAttribute() != null) .ToArray(); + using var consoleWrapper = new ConsoleWrapper(); + + // ReSharper disable AccessToDisposedClosure CommandLine.Parser.Default.ParseArguments(args, verbTypes.ToArray()) .WithParsed(o => { @@ -28,33 +31,33 @@ namespace Tapeti.Cmd // Should have been validated by the ExecutableVerbAttribute Debug.Assert(executer != null, nameof(executer) + " != null"); - using var consoleWrapper = new ConsoleWrapper(); - executer.Execute(consoleWrapper); exitCode = 0; } catch (Exception e) { - Console.WriteLine(e.Message); - DebugConfirmClose(); + using var consoleWriter = consoleWrapper.GetPermanentWriter(); + consoleWriter.WriteLine(e.Message); + DebugConfirmClose(consoleWrapper); } }) .WithNotParsed(_ => { - DebugConfirmClose(); + DebugConfirmClose(consoleWrapper); }); - + // ReSharper restore AccessToDisposedClosure + return exitCode; } - private static void DebugConfirmClose() + private static void DebugConfirmClose(IConsole console) { if (!Debugger.IsAttached) return; - Console.WriteLine("Press any Enter key to continue..."); - Console.ReadLine(); + using var consoleWriter = console.GetPermanentWriter(); + consoleWriter.Confirm("Press any key to continue..."); } } } diff --git a/Tapeti.Cmd/Verbs/BaseConnectionOptions.cs b/Tapeti.Cmd/Verbs/BaseConnectionOptions.cs index 2f09302..6c3bcd1 100644 --- a/Tapeti.Cmd/Verbs/BaseConnectionOptions.cs +++ b/Tapeti.Cmd/Verbs/BaseConnectionOptions.cs @@ -1,22 +1,93 @@ -using CommandLine; +using System; +using CommandLine; +using RabbitMQ.Client; +using Tapeti.Cmd.ConsoleHelper; namespace Tapeti.Cmd.Verbs { public class BaseConnectionOptions { - [Option('h', "host", HelpText = "Hostname of the RabbitMQ server.", Default = "localhost")] + [Option('h', "host", HelpText = "(Default: localhost) Hostname of the RabbitMQ server. Can also be set using the TAPETI_HOST environment variable.")] public string Host { get; set; } - [Option("port", HelpText = "AMQP port of the RabbitMQ server.", Default = 5672)] - public int Port { get; set; } + [Option("port", HelpText = "(Default: 5672) AMQP port of the RabbitMQ server. Can also be set using the TAPETI_PORT environment variable.")] + public int? Port { get; set; } - [Option('v', "virtualhost", HelpText = "Virtual host used for the RabbitMQ connection.", Default = "/")] + [Option('v', "virtualhost", HelpText = "(Default: /) Virtual host used for the RabbitMQ connection. Can also be set using the TAPETI_VIRTUALHOST environment variable.")] public string VirtualHost { get; set; } - [Option('u', "username", HelpText = "Username used to connect to the RabbitMQ server.", Default = "guest")] + [Option('u', "username", HelpText = "(Default: guest) Username used to connect to the RabbitMQ server. Can also be set using the TAPETI_USERNAME environment variable.")] public string Username { get; set; } - [Option('p', "password", HelpText = "Password used to connect to the RabbitMQ server.", Default = "guest")] + [Option('p', "password", HelpText = "(Default: guest) Password used to connect to the RabbitMQ server. Can also be set using the TAPETI_PASSWORD environment variable.")] public string Password { get; set; } + + + public ConnectionFactory CreateConnectionFactory(IConsole console) + { + var consoleWriter = console.GetPermanentWriter(); + consoleWriter.WriteLine("Using connection parameters:"); + + var factory = new ConnectionFactory + { + HostName = GetOptionOrEnvironmentValue(consoleWriter, " Host : ", Host, "TAPETI_HOST", "localhost"), + Port = GetOptionOrEnvironmentValue(consoleWriter, " Port : ", Port, "TAPETI_PORT", 5672), + VirtualHost = GetOptionOrEnvironmentValue(consoleWriter, " Virtual host : ", VirtualHost, "TAPETI_VIRTUALHOST", "/"), + UserName = GetOptionOrEnvironmentValue(consoleWriter, " Username : ", Username, "TAPETI_USERNAME", "guest"), + Password = GetOptionOrEnvironmentValue(consoleWriter, " Password : ", Password, "TAPETI_PASSWORD", "guest", true) + }; + + consoleWriter.WriteLine(""); + return factory; + } + + + private static string GetOptionOrEnvironmentValue(IConsoleWriter consoleWriter, string consoleDisplayName, string optionValue, string environmentName, string defaultValue, bool hideValue = false) + { + string GetDisplayValue(string value) + { + return hideValue + ? "" + : value; + } + + if (!string.IsNullOrEmpty(optionValue)) + { + consoleWriter.WriteLine($"{consoleDisplayName}{GetDisplayValue(optionValue)} (from command-line)"); + return optionValue; + } + + var environmentValue = Environment.GetEnvironmentVariable(environmentName); + if (!string.IsNullOrEmpty(environmentValue)) + { + consoleWriter.WriteLine($"{consoleDisplayName}{GetDisplayValue(environmentValue)} (from environment variable)"); + return environmentValue; + } + + consoleWriter.WriteLine($"{consoleDisplayName}{GetDisplayValue(defaultValue)} (default)"); + return defaultValue; + } + + + private static int GetOptionOrEnvironmentValue(IConsoleWriter consoleWriter, string consoleDisplayName, int? optionValue, string environmentName, int defaultValue) + { + if (optionValue.HasValue) + { + consoleWriter.WriteLine($"{consoleDisplayName}{optionValue} (from command-line)"); + return optionValue.Value; + } + + + var environmentValue = Environment.GetEnvironmentVariable(environmentName); + if (!string.IsNullOrEmpty(environmentValue) && int.TryParse(environmentValue, out var environmentIntValue)) + { + consoleWriter.WriteLine($"{consoleDisplayName}{environmentIntValue} (from environment variable)"); + return environmentIntValue; + } + + consoleWriter.WriteLine($"{consoleDisplayName}{defaultValue} (default)"); + return defaultValue; + } + } } diff --git a/Tapeti.Cmd/Verbs/BindQueueVerb.cs b/Tapeti.Cmd/Verbs/BindQueueVerb.cs index 32c9965..02bbc46 100644 --- a/Tapeti.Cmd/Verbs/BindQueueVerb.cs +++ b/Tapeti.Cmd/Verbs/BindQueueVerb.cs @@ -35,15 +35,7 @@ namespace Tapeti.Cmd.Verbs var consoleWriter = console.GetPermanentWriter(); var bindings = BindingParser.Parse(options.Bindings); - var factory = new ConnectionFactory - { - HostName = options.Host, - Port = options.Port, - VirtualHost = options.VirtualHost, - UserName = options.Username, - Password = options.Password - }; - + var factory = options.CreateConnectionFactory(console); using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); diff --git a/Tapeti.Cmd/Verbs/DeclareQueueVerb.cs b/Tapeti.Cmd/Verbs/DeclareQueueVerb.cs index a119233..b3d3c3e 100644 --- a/Tapeti.Cmd/Verbs/DeclareQueueVerb.cs +++ b/Tapeti.Cmd/Verbs/DeclareQueueVerb.cs @@ -37,15 +37,7 @@ namespace Tapeti.Cmd.Verbs // Parse early to fail early var bindings = BindingParser.Parse(options.Bindings); - var factory = new ConnectionFactory - { - HostName = options.Host, - Port = options.Port, - VirtualHost = options.VirtualHost, - UserName = options.Username, - Password = options.Password - }; - + var factory = options.CreateConnectionFactory(console); using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); diff --git a/Tapeti.Cmd/Verbs/ExportVerb.cs b/Tapeti.Cmd/Verbs/ExportVerb.cs index ac687cd..d6313ef 100644 --- a/Tapeti.Cmd/Verbs/ExportVerb.cs +++ b/Tapeti.Cmd/Verbs/ExportVerb.cs @@ -40,15 +40,7 @@ namespace Tapeti.Cmd.Verbs public void Execute(IConsole console) { var consoleWriter = console.GetPermanentWriter(); - var factory = new ConnectionFactory - { - HostName = options.Host, - Port = options.Port, - VirtualHost = options.VirtualHost, - UserName = options.Username, - Password = options.Password - }; - + var factory = options.CreateConnectionFactory(console); using var messageSerializer = GetMessageSerializer(options); using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); diff --git a/Tapeti.Cmd/Verbs/ImportVerb.cs b/Tapeti.Cmd/Verbs/ImportVerb.cs index 9b4ce3a..87fa434 100644 --- a/Tapeti.Cmd/Verbs/ImportVerb.cs +++ b/Tapeti.Cmd/Verbs/ImportVerb.cs @@ -54,16 +54,7 @@ namespace Tapeti.Cmd.Verbs public void Execute(IConsole console) { var consoleWriter = console.GetPermanentWriter(); - var factory = new ConnectionFactory - { - HostName = options.Host, - Port = options.Port, - VirtualHost = options.VirtualHost, - UserName = options.Username, - Password = options.Password - }; - - using var messageSerializer = GetMessageSerializer(options); + var factory = options.CreateConnectionFactory(console); using var messageSerializer = GetMessageSerializer(options); using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); var rateLimiter = RateLimiterFactory.Create(console, options.MaxRate, options.BatchSize, options.BatchPauseTime); diff --git a/Tapeti.Cmd/Verbs/PurgeVerb.cs b/Tapeti.Cmd/Verbs/PurgeVerb.cs index 9d7edd2..7bdb098 100644 --- a/Tapeti.Cmd/Verbs/PurgeVerb.cs +++ b/Tapeti.Cmd/Verbs/PurgeVerb.cs @@ -37,16 +37,7 @@ namespace Tapeti.Cmd.Verbs return; } - var factory = new ConnectionFactory - { - HostName = options.Host, - Port = options.Port, - VirtualHost = options.VirtualHost, - UserName = options.Username, - Password = options.Password - }; - - using var connection = factory.CreateConnection(); + var factory = options.CreateConnectionFactory(console); using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); var messageCount = channel.QueuePurge(options.QueueName); diff --git a/Tapeti.Cmd/Verbs/RemoveQueueVerb.cs b/Tapeti.Cmd/Verbs/RemoveQueueVerb.cs index 9b7695b..e385cf1 100644 --- a/Tapeti.Cmd/Verbs/RemoveQueueVerb.cs +++ b/Tapeti.Cmd/Verbs/RemoveQueueVerb.cs @@ -41,15 +41,7 @@ namespace Tapeti.Cmd.Verbs return; } - var factory = new ConnectionFactory - { - HostName = options.Host, - Port = options.Port, - VirtualHost = options.VirtualHost, - UserName = options.Username, - Password = options.Password - }; - + var factory = options.CreateConnectionFactory(console); uint messageCount; try diff --git a/Tapeti.Cmd/Verbs/ShovelVerb.cs b/Tapeti.Cmd/Verbs/ShovelVerb.cs index 08b96a3..891abe8 100644 --- a/Tapeti.Cmd/Verbs/ShovelVerb.cs +++ b/Tapeti.Cmd/Verbs/ShovelVerb.cs @@ -61,15 +61,7 @@ namespace Tapeti.Cmd.Verbs public void Execute(IConsole console) { - var sourceFactory = new ConnectionFactory - { - HostName = options.Host, - Port = options.Port, - VirtualHost = options.VirtualHost, - UserName = options.Username, - Password = options.Password - }; - + var sourceFactory = options.CreateConnectionFactory(console); using var sourceConnection = sourceFactory.CreateConnection(); using var sourceChannel = sourceConnection.CreateModel(); @@ -78,7 +70,7 @@ namespace Tapeti.Cmd.Verbs var targetFactory = new ConnectionFactory { HostName = !string.IsNullOrEmpty(options.TargetHost) ? options.TargetHost : options.Host, - Port = options.TargetPort ?? options.Port, + Port = options.TargetPort ?? options.Port ?? 5672, VirtualHost = !string.IsNullOrEmpty(options.TargetVirtualHost) ? options.TargetVirtualHost : options.VirtualHost, UserName = !string.IsNullOrEmpty(options.TargetUsername) ? options.TargetUsername : options.Username, Password = !string.IsNullOrEmpty(options.TargetPassword) ? options.TargetPassword : options.Password diff --git a/Tapeti.Cmd/Verbs/UnbindQueueVerb.cs b/Tapeti.Cmd/Verbs/UnbindQueueVerb.cs index 6dbcdeb..b57724c 100644 --- a/Tapeti.Cmd/Verbs/UnbindQueueVerb.cs +++ b/Tapeti.Cmd/Verbs/UnbindQueueVerb.cs @@ -35,15 +35,7 @@ namespace Tapeti.Cmd.Verbs var consoleWriter = console.GetPermanentWriter(); var bindings = BindingParser.Parse(options.Bindings); - var factory = new ConnectionFactory - { - HostName = options.Host, - Port = options.Port, - VirtualHost = options.VirtualHost, - UserName = options.Username, - Password = options.Password - }; - + var factory = options.CreateConnectionFactory(console); using var connection = factory.CreateConnection(); using var channel = connection.CreateModel();