Skip to content

Commit ec28e4f

Browse files
committed
Added ability to select auto-detected envvar connection string
1 parent cab0144 commit ec28e4f

3 files changed

Lines changed: 76 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.1.0] - 2025-02-11
9+
10+
- ✨ Added ability to select auto-detected envvar connection string
11+
812
## [1.0.1] - 2025-02-11
913

10-
- Fixed shutdown cancellation issue
11-
- Fixed terminal corruption at shutdown
14+
- 🐛 Fixed shutdown cancellation issue
15+
- 🐛 Fixed terminal corruption at shutdown
1216

1317
## [1.0.0] - 2025-02-11
1418

15-
First sort of release roughly 4 years after the [first commit](https://github.com/ramonsmits/FakeMessageGen/commit/8c1bd0d689106962ebaefcb77b6ebbde7fea9eb5)
19+
- 🥳 First sort of release roughly 4 years after the [first commit](https://github.com/ramonsmits/FakeMessageGen/commit/8c1bd0d689106962ebaefcb77b6ebbde7fea9eb5)

FakeMessageGen/FakeMessageGen.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<TargetFramework>net9.0</TargetFramework>
2525
<PackageId>NBraceIT.FakeMessageGen</PackageId>
2626
<ToolCommandName>fakemessagegen</ToolCommandName>
27-
<PackageVersion>1.0.1</PackageVersion>
27+
<PackageVersion>1.1.0</PackageVersion>
2828
<PackageOutputPath>./nupkg</PackageOutputPath>
2929
<PackAsTool>true</PackAsTool>
3030
</PropertyGroup>

FakeMessageGen/Program.cs

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Concurrent;
4+
using System.Collections.Generic;
35
using System.Globalization;
4-
using System.IO;
6+
using System.Linq;
57
using System.Text;
68
using System.Threading;
79
using System.Threading.Tasks;
@@ -83,16 +85,32 @@ How many concurrency (batch) sends to allow
8385
connectionstring:
8486
8587
The connection string to use for the destination.
88+
89+
By default the app will detect connection strings from the envvars and present a
90+
list of transport configuration to choose from when launched.
8691
8792
Will probe the format to check if it can assume RabbitMQ, Azure Service Bus,
8893
or Learning transport.
94+
95+
Azure Service Bus:
96+
97+
Are expected to start with "Endpoint="
98+
99+
RabbitMQ:
100+
101+
Are expected to start with "host="
102+
103+
Learning:
104+
105+
Are expected to start with `/` (linux path) or contains `:\` (windows drive)
106+
89107
""");
90108
return;
91109
}
92110

93111
CreateRateGate();
94112

95-
if (SetupTransport()) return;
113+
if (!SetupTransport()) return;
96114

97115
try
98116
{
@@ -163,8 +181,50 @@ async void Do()
163181
await Task.WhenAll(activeTasks.Keys);
164182
}
165183

184+
enum Transports
185+
{
186+
AzureServiceBus,
187+
RabbitMQ,
188+
Learning
189+
}
190+
166191
static bool SetupTransport()
167192
{
193+
var envvars = Environment.GetEnvironmentVariables();
194+
195+
if (string.IsNullOrWhiteSpace(ConnectionString))
196+
{
197+
List<(string key, Transports type, string v)> transports = new();
198+
199+
foreach (var e in envvars.Cast<DictionaryEntry>())
200+
{
201+
var k = (string)e.Key;
202+
var isConnectionString = k.Contains("CONNECTIONSTRING", StringComparison.OrdinalIgnoreCase);
203+
var v = (string)e.Value;
204+
if ((v.StartsWith("/") || v.Contains(":\\")) && isConnectionString) transports.Add((k, Transports.Learning, v));
205+
if (v.StartsWith("Endpoint")) transports.Add((k, Transports.AzureServiceBus, v));
206+
if (v.StartsWith("host=")) transports.Add((k, Transports.RabbitMQ, v));
207+
}
208+
209+
if (transports.Count == 0)
210+
return false;
211+
212+
Console.WriteLine("Select transport configuration:\n\n");
213+
214+
for (int i = 0; i < transports.Count; i++)
215+
{
216+
var cyan = Ansi.GetAnsiColor(ConsoleColor.DarkCyan);
217+
Console.WriteLine($" [{cyan}{i}{Ansi.Reset}] {transports[i].key}: {transports[i].type}");
218+
}
219+
220+
Console.Write("\nOption: ");
221+
222+
var x = int.Parse(Console.ReadLine());
223+
224+
ConnectionString = transports[x].v;
225+
ConnectionString = Environment.ExpandEnvironmentVariables(ConnectionString);
226+
}
227+
168228
if (!string.IsNullOrEmpty(ConnectionString))
169229
{
170230
if (ConnectionString.StartsWith("Endpoint"))
@@ -183,7 +243,6 @@ static bool SetupTransport()
183243
return true;
184244
}
185245

186-
var envvars = Environment.GetEnvironmentVariables();
187246

188247
var hasASB = envvars.Contains("CONNECTIONSTRING_AZURESERVICEBUS");
189248
var hasRMQ = envvars.Contains("CONNECTIONSTRING_RABBITMQ");
@@ -210,26 +269,23 @@ static bool SetupTransport()
210269

211270
static void SetupLearning()
212271
{
213-
var connectionString = Environment.ExpandEnvironmentVariables(Environment.GetEnvironmentVariable("CONNECTIONSTRING_LEARNING")!);
214272
transportDefinition = new LearningTransport
215273
{
216-
StorageDirectory = connectionString
274+
StorageDirectory = ConnectionString
217275
};
218-
queueMetrics = new LearningMetrics(connectionString);
276+
queueMetrics = new LearningMetrics(ConnectionString);
219277
}
220278

221279
static void SetupRabbitMQ()
222280
{
223-
var connectionString = Environment.ExpandEnvironmentVariables(Environment.GetEnvironmentVariable("CONNECTIONSTRING_AZURESERVICEBUS")!);
224-
transportDefinition = new RabbitMQTransport(RoutingTopology.Conventional(QueueType.Quorum), connectionString);
225-
queueMetrics = RabbitMqMetrics.Parse(connectionString);
281+
transportDefinition = new RabbitMQTransport(RoutingTopology.Conventional(QueueType.Quorum), ConnectionString);
282+
queueMetrics = RabbitMqMetrics.Parse(ConnectionString);
226283
}
227284

228285
static void SetupAzureServiceBus()
229286
{
230-
var connectionString = Environment.ExpandEnvironmentVariables(Environment.GetEnvironmentVariable("CONNECTIONSTRING_AZURESERVICEBUS")!);
231-
transportDefinition = new AzureServiceBusTransport(connectionString);
232-
queueMetrics = new ServiceBusMetrics(connectionString);
287+
transportDefinition = new AzureServiceBusTransport(ConnectionString);
288+
queueMetrics = new ServiceBusMetrics(ConnectionString);
233289
}
234290
}
235291

0 commit comments

Comments
 (0)