2021-09-04 09:33:59 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using CommandLine;
|
|
|
|
|
using RabbitMQ.Client;
|
2021-09-04 12:01:03 +00:00
|
|
|
|
using Tapeti.Cmd.ConsoleHelper;
|
2021-09-04 09:33:59 +00:00
|
|
|
|
using Tapeti.Cmd.Parser;
|
|
|
|
|
|
|
|
|
|
namespace Tapeti.Cmd.Verbs
|
|
|
|
|
{
|
2021-09-04 12:01:03 +00:00
|
|
|
|
[Verb("declarequeue", HelpText = "Declares a durable queue without arguments.")]
|
2021-09-04 09:33:59 +00:00
|
|
|
|
[ExecutableVerb(typeof(DeclareQueueVerb))]
|
|
|
|
|
public class DeclareQueueOptions : BaseConnectionOptions
|
|
|
|
|
{
|
|
|
|
|
[Option('q', "queue", Required = true, HelpText = "The name of the queue to declare.")]
|
|
|
|
|
public string QueueName { get; set; }
|
|
|
|
|
|
|
|
|
|
[Option('b', "bindings", Required = false, HelpText = "One or more bindings to add to the queue. Format: <exchange>:<routingKey>")]
|
|
|
|
|
public IEnumerable<string> Bindings { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class DeclareQueueVerb : IVerbExecuter
|
|
|
|
|
{
|
|
|
|
|
private readonly DeclareQueueOptions options;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DeclareQueueVerb(DeclareQueueOptions options)
|
|
|
|
|
{
|
|
|
|
|
this.options = options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-09-04 12:01:03 +00:00
|
|
|
|
public void Execute(IConsole console)
|
2021-09-04 09:33:59 +00:00
|
|
|
|
{
|
2021-09-04 12:01:03 +00:00
|
|
|
|
var consoleWriter = console.GetPermanentWriter();
|
|
|
|
|
|
2021-09-04 09:33:59 +00:00
|
|
|
|
// Parse early to fail early
|
|
|
|
|
var bindings = BindingParser.Parse(options.Bindings);
|
|
|
|
|
|
2021-09-07 13:56:59 +00:00
|
|
|
|
var factory = options.CreateConnectionFactory(console);
|
2021-09-04 09:33:59 +00:00
|
|
|
|
using var connection = factory.CreateConnection();
|
|
|
|
|
using var channel = connection.CreateModel();
|
|
|
|
|
|
|
|
|
|
channel.QueueDeclare(options.QueueName, true, false, false);
|
|
|
|
|
|
|
|
|
|
foreach (var (exchange, routingKey) in bindings)
|
|
|
|
|
channel.QueueBind(options.QueueName, exchange, routingKey);
|
|
|
|
|
|
2021-09-04 12:01:03 +00:00
|
|
|
|
consoleWriter.WriteLine($"Queue {options.QueueName} declared with {bindings.Length} binding{(bindings.Length != 1 ? "s" : "")}.");
|
2021-09-04 09:33:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|