Skip to content

Commit

Permalink
feat: switch to Spectre.Console for a better CLI experience
Browse files Browse the repository at this point in the history
  • Loading branch information
philasmar committed Dec 9, 2024
1 parent 2f94dbc commit 0a9c885
Show file tree
Hide file tree
Showing 22 changed files with 457 additions and 454 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
<PackageReference Include="Blazored.Modal" Version="7.3.1" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="8.0.11" />
</ItemGroup>
Expand Down
15 changes: 0 additions & 15 deletions Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/AppRunner.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.ComponentModel;
using System.Diagnostics;
using Amazon.Lambda.TestTool.Extensions;
using Amazon.Lambda.TestTool.Models;
using Amazon.Lambda.TestTool.Processes;
using Amazon.Lambda.TestTool.Services;
using Spectre.Console.Cli;

namespace Amazon.Lambda.TestTool.Commands;

public sealed class RunCommand(
IToolInteractiveService toolInteractiveService) : AsyncCommand<RunCommand.Settings>
{
public sealed class Settings : CommandSettings
{
[CommandOption("--host <HOST>")]
[Description(
"The hostname or IP address used for the test tool's web interface. Any host other than an explicit IP address or localhost (e.g. '*', '+' or 'example.com') binds to all public IPv4 and IPv6 addresses.")]
[DefaultValue(Constants.DefaultHost)]
public string Host { get; set; } = Constants.DefaultHost;

[CommandOption("-p|--port <PORT>")]
[Description("The port number used for the test tool's web interface.")]
[DefaultValue(Constants.DefaultPort)]
public int Port { get; set; } = Constants.DefaultPort;

[CommandOption("--no-launch-window")]
[Description("Disable auto launching the test tool's web interface in a browser.")]
public bool NoLaunchWindow { get; set; }

[CommandOption("--pause-exit")]
[Description("If set to true the test tool will pause waiting for a key input before exiting. The is useful when executing from an IDE so you can avoid having the output window immediately disappear after executing the Lambda code. The default value is true.")]
public bool PauseExit { get; set; }

[CommandOption("--disable-logs")]
[Description("Disables logging in the application")]
public bool DisableLogs { get; set; }
}

public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{
try
{
var process = TestToolProcess.Startup(settings);

if (!settings.NoLaunchWindow)
{
try
{
var info = new ProcessStartInfo
{
UseShellExecute = true,
FileName = process.ServiceUrl
};
Process.Start(info);
}
catch (Exception e)
{
toolInteractiveService.WriteErrorLine($"Error launching browser: {e.Message}");
}
}

await process.RunningTask;

return CommandReturnCodes.Success;
}
catch (Exception e) when (e.IsExpectedException())
{
toolInteractiveService.WriteErrorLine(string.Empty);
toolInteractiveService.WriteErrorLine(e.Message);

return CommandReturnCodes.UserError;
}
catch (Exception e)
{
// This is a bug
toolInteractiveService.WriteErrorLine(
$"Unhandled exception.{Environment.NewLine}" +
$"This is a bug.{Environment.NewLine}" +
$"Please copy the stack trace below and file a bug at {Constants.LinkGithubRepo}. " +
e.PrettyPrint());

return CommandReturnCodes.UnhandledException;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
@page "/"

@using Amazon.Lambda.TestTool.Commands
@using Amazon.Lambda.TestTool.Services
@using Amazon.Lambda.TestTool.Models
@using Amazon.Lambda.TestTool.SampleRequests;
@using Amazon.Lambda.TestTool.Utilities
@using Microsoft.AspNetCore.Http;

@inject IHttpContextAccessor httpContextAccessor
@inject ApplicationOptions LambdaOptions
@inject IHttpContextAccessor HttpContextAccessor
@inject IRuntimeApiDataStore RuntimeApiModel
@inject IDirectoryManager DirectoryManager

<PageTitle>Lambd Function Tester</PageTitle>

<h2>Lambd Function Tester</h2>

<p>
For Lambda functions written as executable assemblies, i.e. custom runtimes functions and top level statement functions, this page can be used for testing the functions locally.
Set the <b>AWS_LAMBDA_RUNTIME_API</b> environment variable to <b>@httpContextAccessor?.HttpContext?.Request?.Host</b> while debugging executable assembly
Set the <b>AWS_LAMBDA_RUNTIME_API</b> environment variable to <b>@HttpContextAccessor?.HttpContext?.Request?.Host</b> while debugging executable assembly
Lambda function. More information can be found in the <a href="/documentation">documentation</a>.
</p>

Expand All @@ -26,18 +27,18 @@
<label class="" for="sample-requests">Example Requests:</label>
<select class="control" id="sample-requests" @bind="SelectedSampleRequestName">
<option id="@NO_SAMPLE_SELECTED_ID"> -- select a request -- </option>
@if (@SampleRequests.ContainsKey(SampleRequestManager.SAVED_REQUEST_GROUP))
@if (@SampleRequests.ContainsKey(SampleRequestManager.SavedRequestGroup))
{
<optgroup id="saved-select-request-group" label="@SampleRequestManager.SAVED_REQUEST_GROUP">
@foreach (var request in SampleRequests[SampleRequestManager.SAVED_REQUEST_GROUP])
<optgroup id="saved-select-request-group" label="@SampleRequestManager.SavedRequestGroup">
@foreach (var request in SampleRequests[SampleRequestManager.SavedRequestGroup])
{
<option value="@request.Filename">@request.Name</option>
}
</optgroup>
}
@foreach (var group in SampleRequests.Keys)
{
@if (!string.Equals(group, SampleRequestManager.SAVED_REQUEST_GROUP))
@if (!string.Equals(group, SampleRequestManager.SavedRequestGroup))
{
<optgroup label="@group">
@foreach (var request in SampleRequests[group])
Expand Down Expand Up @@ -208,7 +209,7 @@
protected override void OnInitialized()
{
RuntimeApiModel.StateChange += RuntimeApiModelOnStateChange;
this.SampleRequestManager = new SampleRequestManager(LambdaOptions.GetPreferenceDirectory(false));
this.SampleRequestManager = new SampleRequestManager(DirectoryManager.GetCurrentDirectory());
this.SampleRequests = SampleRequestManager.GetSampleRequests();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
@using Amazon.Lambda.TestTool.Models
@using Amazon.Lambda.TestTool.Commands
@using Amazon.Lambda.TestTool.SampleRequests;
@inject ApplicationOptions LambdaOptions
@using Amazon.Lambda.TestTool.Services
@inject IModalService ModalService
@inject IDirectoryManager DirectoryManager

<div class="simple-form" style="min-width: 400px">
<div class="form-group" for="save-request-name">
Expand Down Expand Up @@ -29,8 +30,8 @@

var requestBody = Parameters.Get<string>(PARAMETER_NAME_REQUEST_BODY);

var manager = new SampleRequestManager(this.LambdaOptions.GetPreferenceDirectory(true));
var systemName = manager.SaveRequest(this.SaveRequestName, requestBody);
var manager = new SampleRequestManager(DirectoryManager.GetCurrentDirectory());
var systemName = manager.SaveRequest(SaveRequestName, requestBody);

await BlazoredModal.CloseAsync(ModalResult.Ok(systemName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Amazon.Lambda.TestTool;

public abstract class Constants
{
public const string ToolName = "lambda-test-tool";
public const int DefaultPort = 5050;
public const string DefaultHost = "localhost";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ public static class ServiceCollectionExtensions
public static void AddCustomServices(this IServiceCollection serviceCollection,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
serviceCollection.TryAdd(new ServiceDescriptor(typeof(ICommandFactory), typeof(CommandFactory), lifetime));
serviceCollection.TryAdd(new ServiceDescriptor(typeof(IToolInteractiveService), typeof(ConsoleInteractiveService), lifetime));

serviceCollection.AddSingleton<AppRunner>();
serviceCollection.TryAdd(new ServiceDescriptor(typeof(IDirectoryManager), typeof(DirectoryManager), lifetime));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class EventContainer
{
public enum Status { Queued, Executing, Success, Failure }

private const string defaultFunctionArn = "arn:aws:lambda:us-west-2:123412341234:function:Function";
private const string DefaultFunctionArn = "arn:aws:lambda:us-west-2:123412341234:function:Function";
public string AwsRequestId { get; }
public string EventJson { get; }
public string? ErrorResponse { get; private set; }
Expand All @@ -29,17 +29,18 @@ public Status EventStatus
}

private readonly RuntimeApiDataStore _dataStore;

public EventContainer(RuntimeApiDataStore dataStore, int eventCount, string eventJson)
{
LastUpdated = DateTime.Now;
this._dataStore = dataStore;
this.AwsRequestId = eventCount.ToString("D12");
this.EventJson = eventJson;
_dataStore = dataStore;
AwsRequestId = eventCount.ToString("D12");
EventJson = eventJson;
}

public string FunctionArn
{
get => defaultFunctionArn;
get => DefaultFunctionArn;
}

public void ReportSuccessResponse(string response)
Expand Down
Loading

0 comments on commit 0a9c885

Please sign in to comment.