about summary refs log tree commit diff
path: root/MatrixUtils.RoomUpgradeCLI/Commands/ModifyCommand.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MatrixUtils.RoomUpgradeCLI/Commands/ModifyCommand.cs')
-rw-r--r--MatrixUtils.RoomUpgradeCLI/Commands/ModifyCommand.cs39
1 files changed, 39 insertions, 0 deletions
diff --git a/MatrixUtils.RoomUpgradeCLI/Commands/ModifyCommand.cs b/MatrixUtils.RoomUpgradeCLI/Commands/ModifyCommand.cs
new file mode 100644

index 0000000..3860448 --- /dev/null +++ b/MatrixUtils.RoomUpgradeCLI/Commands/ModifyCommand.cs
@@ -0,0 +1,39 @@ +using System.Text.Json; +using ArcaneLibs.Extensions; +using LibMatrix.Helpers; +using LibMatrix.Homeservers; +using MatrixUtils.RoomUpgradeCLI.Extensions; + +namespace MatrixUtils.RoomUpgradeCLI.Commands; + +public class ModifyCommand(ILogger<ModifyCommand> logger, IHost host, RuntimeContext ctx, AuthenticatedHomeserverGeneric hs) : IHostedService { + public async Task StartAsync(CancellationToken cancellationToken) { + if (ctx.Args.Length <= 2 || ctx.Args.Contains("--help")) { + await PrintHelp(); + return; + } + + var filename = ctx.Args[1]; + if (filename.StartsWith("--")) { + Console.WriteLine("Filename cannot start with --, please provide a valid filename."); + await PrintHelp(); + } + + var rb = ctx.Args.Contains("--upgrade") + ? await JsonSerializer.DeserializeAsync<RoomUpgradeBuilder>(File.OpenRead(filename), cancellationToken: cancellationToken) + : await JsonSerializer.DeserializeAsync<RoomBuilder>(File.OpenRead(filename), cancellationToken: cancellationToken); + await rb!.ApplyRoomUpgradeCLIArgs(hs, ctx.Args[2..], isNewState: false); + await File.WriteAllTextAsync(filename, rb.ToJson(), cancellationToken); + + await host.StopAsync(cancellationToken); + } + + public async Task StopAsync(CancellationToken cancellationToken) { } + + private async Task PrintHelp() { + Console.WriteLine("Usage: new [filename] [options]"); + Console.WriteLine("Options:"); + + await host.StopAsync(); + } +} \ No newline at end of file