Skip to content

Commit

Permalink
Merge pull request #342 from nblumhardt/appinstance-uninstall
Browse files Browse the repository at this point in the history
`seqcli app uninstall`
  • Loading branch information
KodrAus authored Apr 30, 2024
2 parents 1225878 + 334583b commit c563dcd
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
64 changes: 64 additions & 0 deletions src/SeqCli/Cli/Commands/App/UninstallCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using SeqCli.Cli.Features;
using SeqCli.Connection;
using SeqCli.Util;
using Serilog;

namespace SeqCli.Cli.Commands.App;

[Command("app", "uninstall", "Uninstall an app package",
Example = "seqcli app uninstall --package-id 'Seq.App.JsonArchive'")]
// ReSharper disable once UnusedType.Global
class UninstallCommand : Command
{
readonly SeqConnectionFactory _connectionFactory;

string? _packageId, _id;
readonly ConnectionFeature _connection;

public UninstallCommand(SeqConnectionFactory connectionFactory)
{
_connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));

Options.Add(
"package-id=",
"The package id of the app package to uninstall",
packageId => _packageId = ArgumentString.Normalize(packageId));

Options.Add(
"i=|id=",
"The id of a single app package to uninstall",
t => _id = ArgumentString.Normalize(t));

_connection = Enable<ConnectionFeature>();
}

protected override async Task<int> Run()
{
if (_packageId == null && _id == null)
{
Log.Error("A `package-id` or `id` must be specified");
return 1;
}

var connection = _connectionFactory.Connect(_connection);

var toRemove = _id != null ? [await connection.Apps.FindAsync(_id)]
: (await connection.Apps.ListAsync())
.Where(app => _packageId == app.Package.PackageId)
.ToArray();

if (!toRemove.Any())
{
Log.Error("No matching API key was found");
return 1;
}

foreach (var app in toRemove)
await connection.Apps.RemoveAsync(app);

return 0;
}
}
11 changes: 5 additions & 6 deletions src/SeqCli/Cli/Features/EntityIdentityFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using System;
using System.Collections.Generic;
using SeqCli.Util;

namespace SeqCli.Cli.Features;

Expand All @@ -22,8 +23,6 @@ class EntityIdentityFeature : CommandFeature
readonly string _entityName;
readonly string _verb;

string? _title, _id;

public EntityIdentityFeature(string entityName, string verb)
{
_entityName = entityName ?? throw new ArgumentNullException(nameof(entityName));
Expand All @@ -35,12 +34,12 @@ public override void Enable(OptionSet options)
options.Add(
"t=|title=",
$"The title of the {_entityName}(s) to {_verb}",
t => _title = t);
t => Title = ArgumentString.Normalize(t));

options.Add(
"i=|id=",
$"The id of a single {_entityName} to {_verb}",
t => _id = t);
t => Id = ArgumentString.Normalize(t));
}

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

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

public string? Id => string.IsNullOrWhiteSpace(_id) ? null : _id.Trim();
public string? Id { get; private set; }
}
3 changes: 3 additions & 0 deletions test/SeqCli.EndToEnd/App/AppBasicsTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public Task ExecuteAsync(SeqConnection connection, ILogger logger, CliCommandRun
exit = runner.Exec("app update", "--all");
Assert.Equal(0, exit);

exit = runner.Exec("app uninstall", "--package-id Seq.App.EmailPlus");
Assert.Equal(0, exit);

return Task.CompletedTask;
}
}

0 comments on commit c563dcd

Please sign in to comment.