Add login/logout/publish commands

This commit is contained in:
Caelan Sayler
2024-03-08 20:06:46 +00:00
parent 3adc026e64
commit fea1031d1d
12 changed files with 133 additions and 7 deletions

View File

@@ -0,0 +1,10 @@
namespace Velopack.Vpk.Commands;
public class LoginCommand : VelopackServiceCommand
{
public LoginCommand()
: base("login", "Login to Velopack's hosted service.")
{
//Just hiding this for now as it is not ready for mass consumption.
Hidden = true;
}
}

View File

@@ -0,0 +1,16 @@
using Velopack.Packaging.Abstractions;
using Velopack.Packaging.Flow;
namespace Velopack.Vpk.Commands;
#nullable enable
public class LoginCommandRunner(IVelopackFlowServiceClient Client) : ICommand<LoginOptions>
{
public async Task Run(LoginOptions options)
{
await Client.LoginAsync(new() {
VelopackBaseUrl = options.VelopackBaseUrl
});
}
}

View File

@@ -0,0 +1,5 @@
using Velopack.Packaging.Flow;
namespace Velopack.Vpk.Commands;
public sealed class LoginOptions: VelopackServiceOptions;

View File

@@ -0,0 +1,10 @@
namespace Velopack.Vpk.Commands;
public class LogoutCommand : VelopackServiceCommand
{
public LogoutCommand()
: base("logout", "Remove any stored credential to the Velopack service.")
{
//Just hiding this for now as it is not ready for mass consumption.
Hidden = true;
}
}

View File

@@ -0,0 +1,13 @@
using Velopack.Packaging.Abstractions;
using Velopack.Packaging.Flow;
#nullable enable
namespace Velopack.Vpk.Commands;
internal class LogoutCommandRunner(IVelopackFlowServiceClient Client) : ICommand<LogoutOptions>
{
public async Task Run(LogoutOptions options)
{
await Client.LogoutAsync(options);
}
}

View File

@@ -0,0 +1,5 @@
using Velopack.Packaging.Flow;
namespace Velopack.Vpk.Commands;
public sealed class LogoutOptions: VelopackServiceOptions;

View File

@@ -142,7 +142,7 @@ public class LongHelpCommand : CliOption<bool>
output.Add(Text.NewLine);
var commandsTable = CreateTable();
foreach (var cmd in command.Subcommands) {
foreach (var cmd in command.Subcommands.Where(x => !x.Hidden)) {
var columns = GetTwoColumnRowCommand(cmd);
commandsTable.AddRow(new Markup($"[bold]{columns.FirstColumnText}[/]"), new Text(columns.SecondColumnText));
}
@@ -154,7 +154,7 @@ public class LongHelpCommand : CliOption<bool>
return 0;
}
public TwoColumnHelpRow GetTwoColumnRowCommand(CliCommand command)
public static TwoColumnHelpRow GetTwoColumnRowCommand(CliCommand command)
{
if (command is null) {
throw new ArgumentNullException(nameof(command));
@@ -165,7 +165,7 @@ public class LongHelpCommand : CliOption<bool>
return new TwoColumnHelpRow(firstColumnText, secondColumnText);
}
public TwoColumnHelpRow GetTwoColumnRowOption(CliOption symbol)
public static TwoColumnHelpRow GetTwoColumnRowOption(CliOption symbol)
{
if (symbol is null) {
throw new ArgumentNullException(nameof(symbol));
@@ -187,7 +187,7 @@ public class LongHelpCommand : CliOption<bool>
return new TwoColumnHelpRow(firstColumnText, secondColumnText);
}
private string GetUsage(CliCommand command)
private static string GetUsage(CliCommand command)
{
return string.Join(" ", GetUsageParts().Where(x => !string.IsNullOrWhiteSpace(x)));
@@ -230,7 +230,7 @@ public class LongHelpCommand : CliOption<bool>
}
}
private string FormatArgumentUsage(IList<CliArgument> arguments)
private static string FormatArgumentUsage(IList<CliArgument> arguments)
{
var sb = new StringBuilder(arguments.Count * 100);
@@ -281,7 +281,7 @@ public class LongHelpCommand : CliOption<bool>
public static class HelpExtensions
{
public static bool HasArguments(this CliCommand command) => command.Arguments?.Count > 0;
public static bool HasSubcommands(this CliCommand command) => command.Subcommands?.Count > 0;
public static bool HasSubcommands(this CliCommand command) => command.Subcommands?.Where(x => !x.Hidden).Any() == true;
public static bool HasOptions(this CliCommand command) => command.Options?.Count > 0;
internal static IEnumerable<T> RecurseWhileNotNull<T>(

View File

@@ -0,0 +1,32 @@
namespace Velopack.Vpk.Commands;
#nullable enable
public abstract class VelopackBaseCommand : OutputCommand
{
public string? TeamName { get; private set; }
public string? ProjectName { get; private set; }
protected VelopackBaseCommand(string name, string description)
: base(name, description)
{
AddOption<string>((v) => TeamName = v, "--team-name", "-t")
.SetDescription("The name of the team")
.SetRequired();
AddOption<string>((v) => ProjectName = v, "--project-name", "-p")
.SetDescription("The name of the project")
.SetRequired();
}
}
public class VelopackPublishCommand : VelopackBaseCommand
{
public string? Version { get; set; }
public VelopackPublishCommand()
: base ("publish", "Uploads a release to Velopack's hosted service")
{
}
}

View File

@@ -0,0 +1,17 @@
using Velopack.Packaging.Flow;
namespace Velopack.Vpk.Commands;
public abstract class VelopackServiceCommand : BaseCommand
{
public string VelopackBaseUrl { get; private set; }
protected VelopackServiceCommand(string name, string description)
: base(name, description)
{
AddOption<string>(v => VelopackBaseUrl = v, "--baseUrl")
.SetDescription("The base Uri for the Velopack API service.")
.SetArgumentHelpName("URI")
.SetDefault(VelopackServiceOptions.DefaultBaseUrl);
}
}

View File

@@ -1,4 +1,4 @@
using Riok.Mapperly.Abstractions;
using Riok.Mapperly.Abstractions;
using Velopack.Deployment;
using Velopack.Packaging.Commands;
using Velopack.Packaging.Unix.Commands;
@@ -26,6 +26,9 @@ public static partial class OptionMapper
public static partial S3UploadOptions ToOptions(this S3UploadCommand cmd);
public static partial DeltaGenOptions ToOptions(this DeltaGenCommand cmd);
public static partial DeltaPatchOptions ToOptions(this DeltaPatchCommand cmd);
public static partial LoginOptions ToOptions(this LoginCommand cmd);
public static partial LogoutOptions ToOptions(this LogoutCommand cmd);
public static partial VelopackUploadOptions ToOptions(this VelopackPublishCommand cmd);
private static DirectoryInfo StringToDirectoryInfo(string t)
{

View File

@@ -8,6 +8,7 @@ using Velopack.Deployment;
using Velopack.Packaging.Abstractions;
using Velopack.Packaging.Commands;
using Velopack.Packaging.Exceptions;
using Velopack.Packaging.Flow;
using Velopack.Packaging.Unix.Commands;
using Velopack.Packaging.Windows.Commands;
using Velopack.Vpk.Commands;
@@ -64,6 +65,7 @@ public class Program
SetupConfig(builder);
SetupLogging(builder, verbose, legacyConsole, defaultYes);
SetupVelopackService(builder.Services);
var host = builder.Build();
var provider = host.Services;
@@ -96,6 +98,11 @@ public class Program
deltaCommand.AddCommand<DeltaPatchCommand, DeltaPatchCommandRunner, DeltaPatchOptions>(provider);
rootCommand.Add(deltaCommand);
rootCommand.AddCommand<LoginCommand, LoginCommandRunner, LoginOptions>(provider);
rootCommand.AddCommand<LogoutCommand, LogoutCommandRunner, LogoutOptions>(provider);
rootCommand.AddRepositoryUpload<VelopackPublishCommand, VelopackRepository, VelopackUploadOptions>(provider);
var cli = new CliConfiguration(rootCommand);
return await cli.InvokeAsync(args);
}
@@ -126,10 +133,17 @@ public class Program
builder.Services.AddSingleton<IFancyConsole, SpectreConsole>();
conf.WriteTo.Spectre();
}
builder.Services.AddSingleton<IConsole>(sp => sp.GetRequiredService<IFancyConsole>());
Log.Logger = conf.CreateLogger();
builder.Logging.AddSerilog();
}
private static void SetupVelopackService(IServiceCollection services)
{
services.AddSingleton<IVelopackFlowServiceClient, VelopackFlowServiceClient>();
services.AddHttpClient();
}
}
public static class ProgramCommandExtensions

View File

@@ -40,6 +40,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog.Expressions" Version="4.0.0" />