2021-09-07 13:56:59 +00:00
using System ;
using CommandLine ;
using RabbitMQ.Client ;
using Tapeti.Cmd.ConsoleHelper ;
2021-09-04 09:33:59 +00:00
namespace Tapeti.Cmd.Verbs
{
public class BaseConnectionOptions
{
2021-09-07 13:56:59 +00:00
[Option('h', "host", HelpText = "(Default: localhost) Hostname of the RabbitMQ server. Can also be set using the TAPETI_HOST environment variable.")]
2021-09-04 09:33:59 +00:00
public string Host { get ; set ; }
2021-09-07 13:56:59 +00:00
[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 ; }
2021-09-04 09:33:59 +00:00
2021-09-07 13:56:59 +00:00
[Option('v', "virtualhost", HelpText = "(Default: /) Virtual host used for the RabbitMQ connection. Can also be set using the TAPETI_VIRTUALHOST environment variable.")]
2021-09-04 09:33:59 +00:00
public string VirtualHost { get ; set ; }
2021-09-07 13:56:59 +00:00
[Option('u', "username", HelpText = "(Default: guest) Username used to connect to the RabbitMQ server. Can also be set using the TAPETI_USERNAME environment variable.")]
2021-09-04 09:33:59 +00:00
public string Username { get ; set ; }
2021-09-07 13:56:59 +00:00
[Option('p', "password", HelpText = "(Default: guest) Password used to connect to the RabbitMQ server. Can also be set using the TAPETI_PASSWORD environment variable.")]
2021-09-04 09:33:59 +00:00
public string Password { get ; set ; }
2021-09-07 13:56:59 +00:00
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 ;
}
2021-09-04 09:33:59 +00:00
}
}