Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ public override async Task<int> ExecuteAsync(CommandContext context, DeleteTrans
{
if (nsbConfiguration.CurrentTransport.ToLower() == targetTransport)
{
nsbConfiguration.CurrentTransport = "";
await nservicebusConfiguration.UpdateCurrentTransportAsync(settings.Config.Path, "");
console.WriteLine("This removed your active transport, use \"nservicebus transport set\" to select a different one.");
}
nsbConfiguration.Transports = nsbConfiguration.Transports.Where(x => x.Name.ToLower() != targetTransport).ToList();
await nservicebusConfiguration.RemoveTransportAsync(settings.Config.Path, targetTransport);
console.WriteLine($"deleted transport named {targetTransport} from {settings.Config.Path}");
await nservicebusConfiguration.PersistConfiguration(settings.Config.Path, nsbConfiguration);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ public override async Task<int> ExecuteAsync(CommandContext context, SetTranspor
var targetTransport = settings.TransportName.ToLower();
if (nsbConfiguration.Transports.Select(x => x.Name.ToLower()).Contains(targetTransport))
{
nsbConfiguration.CurrentTransport = targetTransport;
await nservicebusConfiguration.UpdateCurrentTransportAsync(settings.Config.Path, targetTransport);
console.WriteLine($"Switched to transport \"{targetTransport}\".");
await nservicebusConfiguration.PersistConfiguration(settings.Config.Path, nsbConfiguration);
}
else
{
Expand Down
53 changes: 53 additions & 0 deletions src/BuslyCLI.Console/Config/INServiceBusConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface INServiceBusConfiguration
Task<NServiceBusConfig> GetUnValidatedConfigurationAsync(string path);

Task PersistConfiguration(string path, NServiceBusConfig config);
Task UpdateCurrentTransportAsync(string path, string newTransport);
Task RemoveTransportAsync(string path, string transportToRemove);
}

public class NServiceBusConfiguration(IDeserializer yamlDeserializer, ISerializer yamlSerializer, IValidator<NServiceBusConfig> validator) : INServiceBusConfiguration
Expand Down Expand Up @@ -45,4 +47,55 @@ public async Task PersistConfiguration(string path, NServiceBusConfig config)
var yaml = yamlSerializer.Serialize(config);
await File.WriteAllTextAsync(path, yaml);
}

public async Task UpdateCurrentTransportAsync(string path, string newTransport)
{
// Load the YAML
var lines = await File.ReadAllLinesAsync(path);

for (int i = 0; i < lines.Length; i++)
{
var line = lines[i].TrimStart();

if (line.StartsWith("current-transport:"))
{
int indent = lines[i].Length - line.Length; // preserve original indent

lines[i] =
new string(' ', indent) +
"current-transport: " + newTransport;

break;
}
}

await File.WriteAllLinesAsync(path, lines);
}

public async Task RemoveTransportAsync(string path, string transportToRemove)
{
var lines = (await File.ReadAllLinesAsync(path)).ToList();

for (int i = 0; i < lines.Count; i++)
{
if (lines[i].TrimStart().StartsWith($"- name: {transportToRemove}"))
{
int indent = lines[i].TakeWhile(char.IsWhiteSpace).Count();
int j = i + 1;

// Continue deleting child lines until indentation decreases
while (j < lines.Count &&
lines[j].TakeWhile(char.IsWhiteSpace).Count() > indent)
{
j++;
}

// Remove the array item block
lines.RemoveRange(i, j - i);
break;
}
}

await File.WriteAllLinesAsync(path, lines);
}
}
1 change: 0 additions & 1 deletion src/BuslyCLI.Console/Spectre/AppConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using BuslyCLI.Commands.Transport;
using Spectre.Console;
using Spectre.Console.Cli;
using YamlDotNet.Core;

namespace BuslyCLI.Spectre;

Expand Down
Loading