From 0b47db69360979f8f0146e15c27b22aaaea07d21 Mon Sep 17 00:00:00 2001 From: gprossliner <6724584+gprossliner@users.noreply.github.com> Date: Thu, 13 Feb 2025 12:59:03 +0100 Subject: [PATCH 1/5] first implementation of `license show` command --- .../Cli/Commands/License/ShowCommand.cs | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/SeqCli/Cli/Commands/License/ShowCommand.cs diff --git a/src/SeqCli/Cli/Commands/License/ShowCommand.cs b/src/SeqCli/Cli/Commands/License/ShowCommand.cs new file mode 100644 index 00000000..1df87dfd --- /dev/null +++ b/src/SeqCli/Cli/Commands/License/ShowCommand.cs @@ -0,0 +1,49 @@ +using System; +using System.IO; +using System.Text; +using System.Threading.Tasks; +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; + private 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."); + + // should we return an error code here? + return 2; + } + + // this shows good data if --json, but `license-server` else + _output.WriteEntity(license); + + return 0; + } +} \ No newline at end of file From 96e7ac888a3f137365997422dd3091c5f3b6d070 Mon Sep 17 00:00:00 2001 From: gprossliner <6724584+gprossliner@users.noreply.github.com> Date: Thu, 13 Feb 2025 13:00:45 +0100 Subject: [PATCH 2/5] code style --- src/SeqCli/Cli/Commands/License/ShowCommand.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/SeqCli/Cli/Commands/License/ShowCommand.cs b/src/SeqCli/Cli/Commands/License/ShowCommand.cs index 1df87dfd..c3ffb936 100644 --- a/src/SeqCli/Cli/Commands/License/ShowCommand.cs +++ b/src/SeqCli/Cli/Commands/License/ShowCommand.cs @@ -18,14 +18,13 @@ class ShowCommand : Command { readonly SeqConnectionFactory _connectionFactory; readonly ConnectionFeature _connection; - private readonly OutputFormatFeature _output; + 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() From 4a024d6af775c1df279abf0c655fe2ccd994caf3 Mon Sep 17 00:00:00 2001 From: gprossliner <6724584+gprossliner@users.noreply.github.com> Date: Thu, 13 Feb 2025 13:31:51 +0100 Subject: [PATCH 3/5] OutputWrapperLicenseEntity for usable text output --- .../Cli/Commands/License/ShowCommand.cs | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/SeqCli/Cli/Commands/License/ShowCommand.cs b/src/SeqCli/Cli/Commands/License/ShowCommand.cs index c3ffb936..934f4868 100644 --- a/src/SeqCli/Cli/Commands/License/ShowCommand.cs +++ b/src/SeqCli/Cli/Commands/License/ShowCommand.cs @@ -2,6 +2,8 @@ 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; @@ -10,7 +12,7 @@ // ReSharper disable once UnusedType.Global -namespace SeqCli.Cli.Commands.License; +namespace SeqCli.Cli.Commands.License; [Command("license", "show", "Shows license applied to the Seq server", Example = "seqcli license show")] @@ -40,9 +42,22 @@ protected override async Task Run() return 2; } - // this shows good data if --json, but `license-server` else - _output.WriteEntity(license); + _output.WriteEntity(_output.Json ? license : new OutputWrapperLicenseEntity(license)); return 0; } + + /// + /// Wraps the license entity for none json output. + /// + class OutputWrapperLicenseEntity : Entity + { + public string Title { get; set; } + + public OutputWrapperLicenseEntity(LicenseEntity license) + { + this.Id = license.SubscriptionId; + this.Title = $"IsValid: {license.IsValid}"; + } + } } \ No newline at end of file From a69afc65b4473f9c0cc79db19e8c93bba7297aa0 Mon Sep 17 00:00:00 2001 From: gprossliner <6724584+gprossliner@users.noreply.github.com> Date: Fri, 14 Feb 2025 11:21:18 +0100 Subject: [PATCH 4/5] edits suggested by @nblumhardt --- src/SeqCli/Cli/Commands/License/ShowCommand.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/SeqCli/Cli/Commands/License/ShowCommand.cs b/src/SeqCli/Cli/Commands/License/ShowCommand.cs index 934f4868..6cac23b3 100644 --- a/src/SeqCli/Cli/Commands/License/ShowCommand.cs +++ b/src/SeqCli/Cli/Commands/License/ShowCommand.cs @@ -37,8 +37,6 @@ protected override async Task Run() if (license == null) { Log.Warning("No license is currently applied to the server."); - - // should we return an error code here? return 2; } @@ -52,12 +50,9 @@ protected override async Task Run() /// class OutputWrapperLicenseEntity : Entity { - public string Title { get; set; } - public OutputWrapperLicenseEntity(LicenseEntity license) { - this.Id = license.SubscriptionId; - this.Title = $"IsValid: {license.IsValid}"; + this.Id = license.LicenseText; } } } \ No newline at end of file From cd43b22a4486e5961305d9c5759a510d8836479f Mon Sep 17 00:00:00 2001 From: gprossliner <6724584+gprossliner@users.noreply.github.com> Date: Fri, 14 Feb 2025 12:59:57 +0100 Subject: [PATCH 5/5] added EndToEnd test for free license --- .../License/LicenceShowTestCase.cs | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 test/SeqCli.EndToEnd/License/LicenceShowTestCase.cs 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