Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SeqCli.Cli.Features;
using SeqCli.Connection;
using Serilog;

namespace SeqCli.Cli.Commands.ApiKey
{
[Command("apikey", "list", "List of API Keys", Example =
"seqcli apikey list")]
class ListCommand : Command
{
private readonly SeqConnectionFactory _connectionFactory;
private readonly ConnectionFeature _connection;

public ListCommand(SeqConnectionFactory connectionFactory)
{
_connectionFactory = connectionFactory;
_connection = Enable<ConnectionFeature>();
}

protected override async Task<int> Run()
{
var connection = _connectionFactory.Connect(_connection);

var apiKeys = await connection.ApiKeys.ListAsync();
Log.Debug("Retrieved ApiKeys {@ApiKeys}", apiKeys);
var data = apiKeys.Select(a => new
{
a.Title,
a.Id,
a.Token,
a.MinimumLevel,
a.AppliedProperties,
a.CanActAsPrincipal,
a.InputFilter,
a.UseServerTimestamps,
a.IsDefault
});

foreach (var apiKey in data)
{
var apiKeyString = JsonConvert.SerializeObject(apiKey);

Console.WriteLine(apiKeyString);
}
return 0;
}
}
}
61 changes: 61 additions & 0 deletions src/SeqCli/Cli/Commands/ApiKey/RemoveCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Seq.Api.Model.Inputs;
using SeqCli.Cli.Features;
using SeqCli.Connection;

namespace SeqCli.Cli.Commands.ApiKey
{
[Command("apikey", "remove", "Remove API Key from the server", Example =
"seqcli apikey remove -t TestApiKey")]
class RemoveCommand : Command
{
private readonly SeqConnectionFactory _connectionFactory;
private readonly ConnectionFeature _connection;
private string _title;
private string _id;

public RemoveCommand(SeqConnectionFactory connectionFactory)
{
_connectionFactory = connectionFactory;
_connection = Enable<ConnectionFeature>();
Options.Add(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is id a worthwhile option/alternative to title? Would help with the ambiguous ones where names overlap.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added Id and put a check in to ensure that they only specify a single value since both could be confusing as to if its removing only when both match or just one.

"t=|title=",
"Remove API Keys with the specified title",
(t) => _title = t);

Options.Add(
"i=|id=",
"Remove API Keys with the specified Id",
(t) => _id = t);
}

protected override async Task<int> Run()
{
if (_title != default && _id != default)
{
Console.WriteLine("You can only specify \"title\" or \"id\" not both");
return -1;
}

var connection = _connectionFactory.Connect(_connection);

var apiKeys = await connection.ApiKeys.ListAsync();
var apiKeyToRemove = apiKeys.Where(ak => ak.Title == _title || ak.Id == _id).ToList();
if (!apiKeyToRemove.Any())
{
Console.WriteLine($"\"{_title}\" API Key doesn't exist");
return -1;
}

foreach (var apiKeyEntity in apiKeyToRemove)
{
await connection.ApiKeys.RemoveAsync(apiKeyEntity);
}
return 0;
}
}
}