Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

license show command #374

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
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
58 changes: 58 additions & 0 deletions src/SeqCli/Cli/Commands/License/ShowCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Seq.Api.Model;
using Seq.Api.Model.License;
using SeqCli.Cli.Features;
using SeqCli.Config;
using SeqCli.Connection;
using SeqCli.Util;
using Serilog;

// ReSharper disable once UnusedType.Global

namespace SeqCli.Cli.Commands.License;

[Command("license", "show", "Shows license applied to the Seq server",
Example = "seqcli license show")]
class ShowCommand : Command
{
readonly SeqConnectionFactory _connectionFactory;
readonly ConnectionFeature _connection;
readonly OutputFormatFeature _output;

public ShowCommand(SeqConnectionFactory connectionFactory, SeqCliConfig config)
{
_connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
_connection = Enable<ConnectionFeature>();
_output = Enable(new OutputFormatFeature(config.Output));
}

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

if (license == null)
{
Log.Warning("No license is currently applied to the server.");
return 2;
}
Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think we should remove this. If there's no license applied to the server, since the output format is effectively just what's displayed in the "License text" control, returning 0 here with no output is fine. (The rest of seqcli follows a fairly similar set of conventions, e.g. seqcli config get -k ... will produce no output if a setting is blank. This makes use in shell scripts easier.)


_output.WriteEntity(_output.Json ? license : new OutputWrapperLicenseEntity(license));

return 0;
}

/// <summary>
/// Wraps the license entity for none json output.
/// </summary>
class OutputWrapperLicenseEntity : Entity
{
public OutputWrapperLicenseEntity(LicenseEntity license)
{
this.Id = license.LicenseText;
}
}
}
36 changes: 36 additions & 0 deletions test/SeqCli.EndToEnd/License/LicenceShowTestCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Seq.Api;
using SeqCli.EndToEnd.Support;
using Serilog;
using Xunit;

namespace SeqCli.EndToEnd.License;

public class LicenseShowTestCase : ICliTestCase
{
readonly TestDataFolder _testDataFolder;

public LicenseShowTestCase(TestDataFolder testDataFolder)
{
_testDataFolder = testDataFolder;
}

public Task ExecuteAsync(SeqConnection connection, ILogger logger, CliCommandRunner runner)
{

// test empty text output if no license is applied
runner.Exec("license show");
Assert.Equal(
"",
runner.LastRunProcess.Output.Trim());

// test json output if no license is applied
runner.Exec("license show --json");
Assert.Contains(
"You're using the free Individual license.",
runner.LastRunProcess.Output.Trim());
return Task.CompletedTask;
}
}