Make the cli nicer

This commit is contained in:
Gervasio Marchand 2022-12-04 18:16:38 -03:00
parent 0dc9d36ff7
commit 4266901008
No known key found for this signature in database
GPG Key ID: B7736CB188DD0A38
6 changed files with 73 additions and 13 deletions

View File

@ -81,12 +81,23 @@ The first time you run this it needs to create a key, you can trigger that using
docker-compose run --rm cli config {relayHost}
```
### List authorized hosts
```
g3rv4@s1:~/docker/FakeRelay$ docker-compose run --rm cli list-instances
┌─────────────────┬──────────────────────────────────────────────────────────────────────────────────────────┐
│ Host │ Key │
├─────────────────┼──────────────────────────────────────────────────────────────────────────────────────────┤
│ m2.g3rv4.com │ KlYKnm9GJcM0B1p8K98vw8FSpWzWOimZ7/3C9kTdWGUmK3xmFEJJwTZ1wqERVTugLH/9alYILFehqu9Ns2MEAw== │
│ mastodon.social │ 1TxL6m1Esx6tnv4EPxscvAmdQN7qSn0nKeyoM7LD8b9mz+GNfrKaHiWgiT3QcNMUA+dWLyWD8qyl1MuKJ+4uHA== │
└─────────────────┴──────────────────────────────────────────────────────────────────────────────────────────┘
```
### Add authorized hosts
You can add hosts, and that will generate their tokens using the `add-host` command. That will output the key:
```
g3rv4@s1:~/docker/FakeRelay$ docker-compose run --rm cli add-host mastodon.social
g3rv4@s1:~/docker/FakeRelay$ docker-compose run --rm cli host add mastodon.social
Key generated for mastodon.social
vti7J0MDDw1O5EPRwfuUafJJjpErhXTwECGEvuw/G4UVWgLXtnrnmPIRRsOcvMD0juwSlvUnchIzgla030AIRw==
```
@ -96,7 +107,7 @@ vti7J0MDDw1O5EPRwfuUafJJjpErhXTwECGEvuw/G4UVWgLXtnrnmPIRRsOcvMD0juwSlvUnchIzgla0
You can use `update-host` to rotate a hosts' key:
```
g3rv4@s1:~/docker/FakeRelay$ docker-compose run --rm cli update-host mastodon.social
g3rv4@s1:~/docker/FakeRelay$ docker-compose run --rm cli host update mastodon.social
Key generated for mastodon.social
wpSX9xpPgX0gjgAxO0Jc+GLSOXubVgv73FOvAihR2EmgK/AfDHz21sF72uqrLnVGzcq2BDXosMeKdFR76q6fpg==
```
@ -106,6 +117,6 @@ wpSX9xpPgX0gjgAxO0Jc+GLSOXubVgv73FOvAihR2EmgK/AfDHz21sF72uqrLnVGzcq2BDXosMeKdFR7
If you want to revoke a host's key, you can use `delete-host`:
```
g3rv4@s1:~/docker/FakeRelay$ docker-compose run --rm cli delete-host mastodon.social
g3rv4@s1:~/docker/FakeRelay$ docker-compose run --rm cli host delete mastodon.social
Key deleted for mastodon.social
```

View File

@ -0,0 +1,32 @@
using FakeRelay.Core.Helpers;
using Spectre.Console;
using Spectre.Console.Cli;
namespace FakeRelay.Cli.Commands;
public class ListInstancesCommand : ConfigEnabledAsyncCommand<EmptyCommandSettings>
{
public override async Task<int> ExecuteAsync(CommandContext context, EmptyCommandSettings settings)
{
var keyToHost = await ApiKeysHelper.GetTokenToHostAsync();
var hostToKeys = keyToHost.ToLookup(d => d.Value, d => d.Key);
// Create a table
var table = new Table();
table.AddColumn("Host");
table.AddColumn("Key");
foreach (var group in hostToKeys)
{
var host = group.Key;
foreach (var key in group)
{
table.AddRow($"[green]{host}[/]", $"[red]{key}[/]");
}
}
AnsiConsole.Write(table);
return 0;
}
}

View File

@ -1,16 +1,25 @@
using FakeRelay.Cli.Commands;
using FakeRelay.Cli.Settings;
using Spectre.Console.Cli;
var app = new CommandApp();
app.Configure(config =>
{
config.AddCommand<AddHostCommand>("add-host")
.WithDescription("Adds a host to the relay and generates a key.")
.WithExample(new[] {"mastodon.social"});
config.AddBranch<EmptyBaseSettings>("host", host =>
{
host.AddCommand<AddHostCommand>("add")
.WithDescription("Adds a host to the relay and generates a key.");
host.AddCommand<UpdateHostCommand>("update")
.WithDescription("Generates a new key for the host. The old one can't be used anymore.");
host.AddCommand<DeleteHostCommand>("delete")
.WithDescription("Deletes the existing keys for the host. They can't use FakeRelay anymore.");
});
config.AddCommand<UpdateHostCommand>("update-host");
config.AddCommand<DeleteHostCommand>("delete-host");
config.AddCommand<ConfigCommand>("config");
config.AddCommand<ListInstancesCommand>("list-instances")
.WithDescription("Lists the hosts and their keys");
config.AddCommand<ConfigCommand>("config")
.WithDescription("Initializes the FakeRelay configuration.");;
});
return app.Run(args);

View File

@ -6,6 +6,6 @@ namespace FakeRelay.Cli.Settings;
public class ConfigSettings : CommandSettings
{
[Description("The hostname of the relay.")]
[CommandArgument(0, "<HOST>")]
[CommandArgument(0, "<FAKERELAY_HOST>")]
public string Host { get; set; }
}

View File

@ -0,0 +1,8 @@
using Spectre.Console.Cli;
namespace FakeRelay.Cli.Settings;
public class EmptyBaseSettings : CommandSettings
{
}

View File

@ -3,9 +3,9 @@ using Spectre.Console.Cli;
namespace FakeRelay.Cli.Settings;
public class HostSettings : CommandSettings
public class HostSettings : EmptyBaseSettings
{
[Description("The instance that connects to this fake relay.")]
[CommandArgument(0, "<HOST>")]
[Description("The instance that connects to this FakeRelay.")]
[CommandArgument(0, "<INSTANCE_HOST>")]
public string Host { get; set; }
}