Skip to content

Commit 334583b

Browse files
committed
seqcli app uninstall
1 parent 1225878 commit 334583b

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}

src/SeqCli/Cli/Features/EntityIdentityFeature.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using System;
1616
using System.Collections.Generic;
17+
using SeqCli.Util;
1718

1819
namespace SeqCli.Cli.Features;
1920

@@ -22,8 +23,6 @@ class EntityIdentityFeature : CommandFeature
2223
readonly string _entityName;
2324
readonly string _verb;
2425

25-
string? _title, _id;
26-
2726
public EntityIdentityFeature(string entityName, string verb)
2827
{
2928
_entityName = entityName ?? throw new ArgumentNullException(nameof(entityName));
@@ -35,12 +34,12 @@ public override void Enable(OptionSet options)
3534
options.Add(
3635
"t=|title=",
3736
$"The title of the {_entityName}(s) to {_verb}",
38-
t => _title = t);
37+
t => Title = ArgumentString.Normalize(t));
3938

4039
options.Add(
4140
"i=|id=",
4241
$"The id of a single {_entityName} to {_verb}",
43-
t => _id = t);
42+
t => Id = ArgumentString.Normalize(t));
4443
}
4544

4645
public override IEnumerable<string> GetUsageErrors()
@@ -49,7 +48,7 @@ public override IEnumerable<string> GetUsageErrors()
4948
yield return "Only one of either `title` or `id` can be specified";
5049
}
5150

52-
public string? Title => string.IsNullOrWhiteSpace(_title) ? null : _title.Trim();
51+
public string? Title { get; private set; }
5352

54-
public string? Id => string.IsNullOrWhiteSpace(_id) ? null : _id.Trim();
53+
public string? Id { get; private set; }
5554
}

test/SeqCli.EndToEnd/App/AppBasicsTestCase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public Task ExecuteAsync(SeqConnection connection, ILogger logger, CliCommandRun
2323
exit = runner.Exec("app update", "--all");
2424
Assert.Equal(0, exit);
2525

26+
exit = runner.Exec("app uninstall", "--package-id Seq.App.EmailPlus");
27+
Assert.Equal(0, exit);
28+
2629
return Task.CompletedTask;
2730
}
2831
}

0 commit comments

Comments
 (0)