diff --git a/src/SeqCli/Cli/Commands/License/ShowCommand.cs b/src/SeqCli/Cli/Commands/License/ShowCommand.cs new file mode 100644 index 00000000..6cac23b3 --- /dev/null +++ b/src/SeqCli/Cli/Commands/License/ShowCommand.cs @@ -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(); + _output = Enable(new OutputFormatFeature(config.Output)); + } + + protected override async Task 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; + } + + _output.WriteEntity(_output.Json ? license : new OutputWrapperLicenseEntity(license)); + + return 0; + } + + /// + /// Wraps the license entity for none json output. + /// + class OutputWrapperLicenseEntity : Entity + { + public OutputWrapperLicenseEntity(LicenseEntity license) + { + this.Id = license.LicenseText; + } + } +} \ No newline at end of file diff --git a/test/SeqCli.EndToEnd/License/LicenceShowTestCase.cs b/test/SeqCli.EndToEnd/License/LicenceShowTestCase.cs new file mode 100644 index 00000000..a58aeda5 --- /dev/null +++ b/test/SeqCli.EndToEnd/License/LicenceShowTestCase.cs @@ -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; + } +} \ No newline at end of file