|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using SeqCli.Cli.Features; |
| 5 | +using SeqCli.Connection; |
| 6 | +using SeqCli.Util; |
| 7 | +using Serilog; |
| 8 | + |
| 9 | +namespace SeqCli.Cli.Commands.App; |
| 10 | + |
| 11 | +[Command("app", "uninstall", "Uninstall an app package", |
| 12 | + Example = "seqcli app uninstall --package-id 'Seq.App.JsonArchive'")] |
| 13 | +// ReSharper disable once UnusedType.Global |
| 14 | +class UninstallCommand : Command |
| 15 | +{ |
| 16 | + readonly SeqConnectionFactory _connectionFactory; |
| 17 | + |
| 18 | + string? _packageId, _id; |
| 19 | + readonly ConnectionFeature _connection; |
| 20 | + |
| 21 | + public UninstallCommand(SeqConnectionFactory connectionFactory) |
| 22 | + { |
| 23 | + _connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory)); |
| 24 | + |
| 25 | + Options.Add( |
| 26 | + "package-id=", |
| 27 | + "The package id of the app package to uninstall", |
| 28 | + packageId => _packageId = ArgumentString.Normalize(packageId)); |
| 29 | + |
| 30 | + Options.Add( |
| 31 | + "i=|id=", |
| 32 | + "The id of a single app package to uninstall", |
| 33 | + t => _id = ArgumentString.Normalize(t)); |
| 34 | + |
| 35 | + _connection = Enable<ConnectionFeature>(); |
| 36 | + } |
| 37 | + |
| 38 | + protected override async Task<int> Run() |
| 39 | + { |
| 40 | + if (_packageId == null && _id == null) |
| 41 | + { |
| 42 | + Log.Error("A `package-id` or `id` must be specified"); |
| 43 | + return 1; |
| 44 | + } |
| 45 | + |
| 46 | + var connection = _connectionFactory.Connect(_connection); |
| 47 | + |
| 48 | + var toRemove = _id != null ? [await connection.Apps.FindAsync(_id)] |
| 49 | + : (await connection.Apps.ListAsync()) |
| 50 | + .Where(app => _packageId == app.Package.PackageId) |
| 51 | + .ToArray(); |
| 52 | + |
| 53 | + if (!toRemove.Any()) |
| 54 | + { |
| 55 | + Log.Error("No matching API key was found"); |
| 56 | + return 1; |
| 57 | + } |
| 58 | + |
| 59 | + foreach (var app in toRemove) |
| 60 | + await connection.Apps.RemoveAsync(app); |
| 61 | + |
| 62 | + return 0; |
| 63 | + } |
| 64 | +} |
0 commit comments