Allow environment variables for Tapeti.Cmd connection parameters
Output connection parameters and their sources to the console
This commit is contained in:
parent
837f48ebb8
commit
f16921d12d
@ -17,6 +17,9 @@ namespace Tapeti.Cmd
|
|||||||
.Where(t => t.GetCustomAttribute<ExecutableVerbAttribute>() != null)
|
.Where(t => t.GetCustomAttribute<ExecutableVerbAttribute>() != null)
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
|
using var consoleWrapper = new ConsoleWrapper();
|
||||||
|
|
||||||
|
// ReSharper disable AccessToDisposedClosure
|
||||||
CommandLine.Parser.Default.ParseArguments(args, verbTypes.ToArray())
|
CommandLine.Parser.Default.ParseArguments(args, verbTypes.ToArray())
|
||||||
.WithParsed(o =>
|
.WithParsed(o =>
|
||||||
{
|
{
|
||||||
@ -28,33 +31,33 @@ namespace Tapeti.Cmd
|
|||||||
// Should have been validated by the ExecutableVerbAttribute
|
// Should have been validated by the ExecutableVerbAttribute
|
||||||
Debug.Assert(executer != null, nameof(executer) + " != null");
|
Debug.Assert(executer != null, nameof(executer) + " != null");
|
||||||
|
|
||||||
using var consoleWrapper = new ConsoleWrapper();
|
|
||||||
|
|
||||||
executer.Execute(consoleWrapper);
|
executer.Execute(consoleWrapper);
|
||||||
exitCode = 0;
|
exitCode = 0;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.WriteLine(e.Message);
|
using var consoleWriter = consoleWrapper.GetPermanentWriter();
|
||||||
DebugConfirmClose();
|
consoleWriter.WriteLine(e.Message);
|
||||||
|
DebugConfirmClose(consoleWrapper);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.WithNotParsed(_ =>
|
.WithNotParsed(_ =>
|
||||||
{
|
{
|
||||||
DebugConfirmClose();
|
DebugConfirmClose(consoleWrapper);
|
||||||
});
|
});
|
||||||
|
// ReSharper restore AccessToDisposedClosure
|
||||||
|
|
||||||
return exitCode;
|
return exitCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void DebugConfirmClose()
|
private static void DebugConfirmClose(IConsole console)
|
||||||
{
|
{
|
||||||
if (!Debugger.IsAttached)
|
if (!Debugger.IsAttached)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Console.WriteLine("Press any Enter key to continue...");
|
using var consoleWriter = console.GetPermanentWriter();
|
||||||
Console.ReadLine();
|
consoleWriter.Confirm("Press any key to continue...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,93 @@
|
|||||||
using CommandLine;
|
using System;
|
||||||
|
using CommandLine;
|
||||||
|
using RabbitMQ.Client;
|
||||||
|
using Tapeti.Cmd.ConsoleHelper;
|
||||||
|
|
||||||
namespace Tapeti.Cmd.Verbs
|
namespace Tapeti.Cmd.Verbs
|
||||||
{
|
{
|
||||||
public class BaseConnectionOptions
|
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; }
|
public string Host { get; set; }
|
||||||
|
|
||||||
[Option("port", HelpText = "AMQP port of the RabbitMQ server.", Default = 5672)]
|
[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; }
|
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; }
|
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; }
|
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 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
|
||||||
|
? "<hidden>"
|
||||||
|
: 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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,15 +35,7 @@ namespace Tapeti.Cmd.Verbs
|
|||||||
var consoleWriter = console.GetPermanentWriter();
|
var consoleWriter = console.GetPermanentWriter();
|
||||||
var bindings = BindingParser.Parse(options.Bindings);
|
var bindings = BindingParser.Parse(options.Bindings);
|
||||||
|
|
||||||
var factory = new ConnectionFactory
|
var factory = options.CreateConnectionFactory(console);
|
||||||
{
|
|
||||||
HostName = options.Host,
|
|
||||||
Port = options.Port,
|
|
||||||
VirtualHost = options.VirtualHost,
|
|
||||||
UserName = options.Username,
|
|
||||||
Password = options.Password
|
|
||||||
};
|
|
||||||
|
|
||||||
using var connection = factory.CreateConnection();
|
using var connection = factory.CreateConnection();
|
||||||
using var channel = connection.CreateModel();
|
using var channel = connection.CreateModel();
|
||||||
|
|
||||||
|
@ -37,15 +37,7 @@ namespace Tapeti.Cmd.Verbs
|
|||||||
// Parse early to fail early
|
// Parse early to fail early
|
||||||
var bindings = BindingParser.Parse(options.Bindings);
|
var bindings = BindingParser.Parse(options.Bindings);
|
||||||
|
|
||||||
var factory = new ConnectionFactory
|
var factory = options.CreateConnectionFactory(console);
|
||||||
{
|
|
||||||
HostName = options.Host,
|
|
||||||
Port = options.Port,
|
|
||||||
VirtualHost = options.VirtualHost,
|
|
||||||
UserName = options.Username,
|
|
||||||
Password = options.Password
|
|
||||||
};
|
|
||||||
|
|
||||||
using var connection = factory.CreateConnection();
|
using var connection = factory.CreateConnection();
|
||||||
using var channel = connection.CreateModel();
|
using var channel = connection.CreateModel();
|
||||||
|
|
||||||
|
@ -40,15 +40,7 @@ namespace Tapeti.Cmd.Verbs
|
|||||||
public void Execute(IConsole console)
|
public void Execute(IConsole console)
|
||||||
{
|
{
|
||||||
var consoleWriter = console.GetPermanentWriter();
|
var consoleWriter = console.GetPermanentWriter();
|
||||||
var factory = new ConnectionFactory
|
var factory = options.CreateConnectionFactory(console);
|
||||||
{
|
|
||||||
HostName = options.Host,
|
|
||||||
Port = options.Port,
|
|
||||||
VirtualHost = options.VirtualHost,
|
|
||||||
UserName = options.Username,
|
|
||||||
Password = options.Password
|
|
||||||
};
|
|
||||||
|
|
||||||
using var messageSerializer = GetMessageSerializer(options);
|
using var messageSerializer = GetMessageSerializer(options);
|
||||||
using var connection = factory.CreateConnection();
|
using var connection = factory.CreateConnection();
|
||||||
using var channel = connection.CreateModel();
|
using var channel = connection.CreateModel();
|
||||||
|
@ -54,16 +54,7 @@ namespace Tapeti.Cmd.Verbs
|
|||||||
public void Execute(IConsole console)
|
public void Execute(IConsole console)
|
||||||
{
|
{
|
||||||
var consoleWriter = console.GetPermanentWriter();
|
var consoleWriter = console.GetPermanentWriter();
|
||||||
var factory = new ConnectionFactory
|
var factory = options.CreateConnectionFactory(console); using var messageSerializer = GetMessageSerializer(options);
|
||||||
{
|
|
||||||
HostName = options.Host,
|
|
||||||
Port = options.Port,
|
|
||||||
VirtualHost = options.VirtualHost,
|
|
||||||
UserName = options.Username,
|
|
||||||
Password = options.Password
|
|
||||||
};
|
|
||||||
|
|
||||||
using var messageSerializer = GetMessageSerializer(options);
|
|
||||||
using var connection = factory.CreateConnection();
|
using var connection = factory.CreateConnection();
|
||||||
using var channel = connection.CreateModel();
|
using var channel = connection.CreateModel();
|
||||||
var rateLimiter = RateLimiterFactory.Create(console, options.MaxRate, options.BatchSize, options.BatchPauseTime);
|
var rateLimiter = RateLimiterFactory.Create(console, options.MaxRate, options.BatchSize, options.BatchPauseTime);
|
||||||
|
@ -37,16 +37,7 @@ namespace Tapeti.Cmd.Verbs
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var factory = new ConnectionFactory
|
var factory = options.CreateConnectionFactory(console); using var connection = factory.CreateConnection();
|
||||||
{
|
|
||||||
HostName = options.Host,
|
|
||||||
Port = options.Port,
|
|
||||||
VirtualHost = options.VirtualHost,
|
|
||||||
UserName = options.Username,
|
|
||||||
Password = options.Password
|
|
||||||
};
|
|
||||||
|
|
||||||
using var connection = factory.CreateConnection();
|
|
||||||
using var channel = connection.CreateModel();
|
using var channel = connection.CreateModel();
|
||||||
|
|
||||||
var messageCount = channel.QueuePurge(options.QueueName);
|
var messageCount = channel.QueuePurge(options.QueueName);
|
||||||
|
@ -41,15 +41,7 @@ namespace Tapeti.Cmd.Verbs
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var factory = new ConnectionFactory
|
var factory = options.CreateConnectionFactory(console);
|
||||||
{
|
|
||||||
HostName = options.Host,
|
|
||||||
Port = options.Port,
|
|
||||||
VirtualHost = options.VirtualHost,
|
|
||||||
UserName = options.Username,
|
|
||||||
Password = options.Password
|
|
||||||
};
|
|
||||||
|
|
||||||
uint messageCount;
|
uint messageCount;
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -61,15 +61,7 @@ namespace Tapeti.Cmd.Verbs
|
|||||||
|
|
||||||
public void Execute(IConsole console)
|
public void Execute(IConsole console)
|
||||||
{
|
{
|
||||||
var sourceFactory = new ConnectionFactory
|
var sourceFactory = options.CreateConnectionFactory(console);
|
||||||
{
|
|
||||||
HostName = options.Host,
|
|
||||||
Port = options.Port,
|
|
||||||
VirtualHost = options.VirtualHost,
|
|
||||||
UserName = options.Username,
|
|
||||||
Password = options.Password
|
|
||||||
};
|
|
||||||
|
|
||||||
using var sourceConnection = sourceFactory.CreateConnection();
|
using var sourceConnection = sourceFactory.CreateConnection();
|
||||||
using var sourceChannel = sourceConnection.CreateModel();
|
using var sourceChannel = sourceConnection.CreateModel();
|
||||||
|
|
||||||
@ -78,7 +70,7 @@ namespace Tapeti.Cmd.Verbs
|
|||||||
var targetFactory = new ConnectionFactory
|
var targetFactory = new ConnectionFactory
|
||||||
{
|
{
|
||||||
HostName = !string.IsNullOrEmpty(options.TargetHost) ? options.TargetHost : options.Host,
|
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,
|
VirtualHost = !string.IsNullOrEmpty(options.TargetVirtualHost) ? options.TargetVirtualHost : options.VirtualHost,
|
||||||
UserName = !string.IsNullOrEmpty(options.TargetUsername) ? options.TargetUsername : options.Username,
|
UserName = !string.IsNullOrEmpty(options.TargetUsername) ? options.TargetUsername : options.Username,
|
||||||
Password = !string.IsNullOrEmpty(options.TargetPassword) ? options.TargetPassword : options.Password
|
Password = !string.IsNullOrEmpty(options.TargetPassword) ? options.TargetPassword : options.Password
|
||||||
|
@ -35,15 +35,7 @@ namespace Tapeti.Cmd.Verbs
|
|||||||
var consoleWriter = console.GetPermanentWriter();
|
var consoleWriter = console.GetPermanentWriter();
|
||||||
var bindings = BindingParser.Parse(options.Bindings);
|
var bindings = BindingParser.Parse(options.Bindings);
|
||||||
|
|
||||||
var factory = new ConnectionFactory
|
var factory = options.CreateConnectionFactory(console);
|
||||||
{
|
|
||||||
HostName = options.Host,
|
|
||||||
Port = options.Port,
|
|
||||||
VirtualHost = options.VirtualHost,
|
|
||||||
UserName = options.Username,
|
|
||||||
Password = options.Password
|
|
||||||
};
|
|
||||||
|
|
||||||
using var connection = factory.CreateConnection();
|
using var connection = factory.CreateConnection();
|
||||||
using var channel = connection.CreateModel();
|
using var channel = connection.CreateModel();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user