Skip to content

Commit 0a9c885

Browse files
committed
feat: switch to Spectre.Console for a better CLI experience
1 parent 2f94dbc commit 0a9c885

File tree

22 files changed

+457
-454
lines changed

22 files changed

+457
-454
lines changed

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Amazon.Lambda.TestTool.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
</PropertyGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
18+
<PackageReference Include="Spectre.Console" Version="0.49.1" />
19+
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
1920
<PackageReference Include="Blazored.Modal" Version="7.3.1" />
2021
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="8.0.11" />
2122
</ItemGroup>

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/AppRunner.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Commands/CommandFactory.cs

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.ComponentModel;
2+
using System.Diagnostics;
3+
using Amazon.Lambda.TestTool.Extensions;
4+
using Amazon.Lambda.TestTool.Models;
5+
using Amazon.Lambda.TestTool.Processes;
6+
using Amazon.Lambda.TestTool.Services;
7+
using Spectre.Console.Cli;
8+
9+
namespace Amazon.Lambda.TestTool.Commands;
10+
11+
public sealed class RunCommand(
12+
IToolInteractiveService toolInteractiveService) : AsyncCommand<RunCommand.Settings>
13+
{
14+
public sealed class Settings : CommandSettings
15+
{
16+
[CommandOption("--host <HOST>")]
17+
[Description(
18+
"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.")]
19+
[DefaultValue(Constants.DefaultHost)]
20+
public string Host { get; set; } = Constants.DefaultHost;
21+
22+
[CommandOption("-p|--port <PORT>")]
23+
[Description("The port number used for the test tool's web interface.")]
24+
[DefaultValue(Constants.DefaultPort)]
25+
public int Port { get; set; } = Constants.DefaultPort;
26+
27+
[CommandOption("--no-launch-window")]
28+
[Description("Disable auto launching the test tool's web interface in a browser.")]
29+
public bool NoLaunchWindow { get; set; }
30+
31+
[CommandOption("--pause-exit")]
32+
[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.")]
33+
public bool PauseExit { get; set; }
34+
35+
[CommandOption("--disable-logs")]
36+
[Description("Disables logging in the application")]
37+
public bool DisableLogs { get; set; }
38+
}
39+
40+
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
41+
{
42+
try
43+
{
44+
var process = TestToolProcess.Startup(settings);
45+
46+
if (!settings.NoLaunchWindow)
47+
{
48+
try
49+
{
50+
var info = new ProcessStartInfo
51+
{
52+
UseShellExecute = true,
53+
FileName = process.ServiceUrl
54+
};
55+
Process.Start(info);
56+
}
57+
catch (Exception e)
58+
{
59+
toolInteractiveService.WriteErrorLine($"Error launching browser: {e.Message}");
60+
}
61+
}
62+
63+
await process.RunningTask;
64+
65+
return CommandReturnCodes.Success;
66+
}
67+
catch (Exception e) when (e.IsExpectedException())
68+
{
69+
toolInteractiveService.WriteErrorLine(string.Empty);
70+
toolInteractiveService.WriteErrorLine(e.Message);
71+
72+
return CommandReturnCodes.UserError;
73+
}
74+
catch (Exception e)
75+
{
76+
// This is a bug
77+
toolInteractiveService.WriteErrorLine(
78+
$"Unhandled exception.{Environment.NewLine}" +
79+
$"This is a bug.{Environment.NewLine}" +
80+
$"Please copy the stack trace below and file a bug at {Constants.LinkGithubRepo}. " +
81+
e.PrettyPrint());
82+
83+
return CommandReturnCodes.UnhandledException;
84+
}
85+
}
86+
}

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Components/Pages/Home.razor

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
@page "/"
22

3+
@using Amazon.Lambda.TestTool.Commands
34
@using Amazon.Lambda.TestTool.Services
45
@using Amazon.Lambda.TestTool.Models
56
@using Amazon.Lambda.TestTool.SampleRequests;
67
@using Amazon.Lambda.TestTool.Utilities
78
@using Microsoft.AspNetCore.Http;
89

9-
@inject IHttpContextAccessor httpContextAccessor
10-
@inject ApplicationOptions LambdaOptions
10+
@inject IHttpContextAccessor HttpContextAccessor
1111
@inject IRuntimeApiDataStore RuntimeApiModel
12+
@inject IDirectoryManager DirectoryManager
1213

1314
<PageTitle>Lambd Function Tester</PageTitle>
1415

1516
<h2>Lambd Function Tester</h2>
1617

1718
<p>
1819
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.
19-
Set the <b>AWS_LAMBDA_RUNTIME_API</b> environment variable to <b>@httpContextAccessor?.HttpContext?.Request?.Host</b> while debugging executable assembly
20+
Set the <b>AWS_LAMBDA_RUNTIME_API</b> environment variable to <b>@HttpContextAccessor?.HttpContext?.Request?.Host</b> while debugging executable assembly
2021
Lambda function. More information can be found in the <a href="/documentation">documentation</a>.
2122
</p>
2223

@@ -26,18 +27,18 @@
2627
<label class="" for="sample-requests">Example Requests:</label>
2728
<select class="control" id="sample-requests" @bind="SelectedSampleRequestName">
2829
<option id="@NO_SAMPLE_SELECTED_ID"> -- select a request -- </option>
29-
@if (@SampleRequests.ContainsKey(SampleRequestManager.SAVED_REQUEST_GROUP))
30+
@if (@SampleRequests.ContainsKey(SampleRequestManager.SavedRequestGroup))
3031
{
31-
<optgroup id="saved-select-request-group" label="@SampleRequestManager.SAVED_REQUEST_GROUP">
32-
@foreach (var request in SampleRequests[SampleRequestManager.SAVED_REQUEST_GROUP])
32+
<optgroup id="saved-select-request-group" label="@SampleRequestManager.SavedRequestGroup">
33+
@foreach (var request in SampleRequests[SampleRequestManager.SavedRequestGroup])
3334
{
3435
<option value="@request.Filename">@request.Name</option>
3536
}
3637
</optgroup>
3738
}
3839
@foreach (var group in SampleRequests.Keys)
3940
{
40-
@if (!string.Equals(group, SampleRequestManager.SAVED_REQUEST_GROUP))
41+
@if (!string.Equals(group, SampleRequestManager.SavedRequestGroup))
4142
{
4243
<optgroup label="@group">
4344
@foreach (var request in SampleRequests[group])
@@ -208,7 +209,7 @@
208209
protected override void OnInitialized()
209210
{
210211
RuntimeApiModel.StateChange += RuntimeApiModelOnStateChange;
211-
this.SampleRequestManager = new SampleRequestManager(LambdaOptions.GetPreferenceDirectory(false));
212+
this.SampleRequestManager = new SampleRequestManager(DirectoryManager.GetCurrentDirectory());
212213
this.SampleRequests = SampleRequestManager.GetSampleRequests();
213214
}
214215

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Components/Pages/SaveRequestDialog.razor

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
@using Amazon.Lambda.TestTool.Models
1+
@using Amazon.Lambda.TestTool.Commands
22
@using Amazon.Lambda.TestTool.SampleRequests;
3-
@inject ApplicationOptions LambdaOptions
3+
@using Amazon.Lambda.TestTool.Services
44
@inject IModalService ModalService
5+
@inject IDirectoryManager DirectoryManager
56

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

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

32-
var manager = new SampleRequestManager(this.LambdaOptions.GetPreferenceDirectory(true));
33-
var systemName = manager.SaveRequest(this.SaveRequestName, requestBody);
33+
var manager = new SampleRequestManager(DirectoryManager.GetCurrentDirectory());
34+
var systemName = manager.SaveRequest(SaveRequestName, requestBody);
3435

3536
await BlazoredModal.CloseAsync(ModalResult.Ok(systemName));
3637
}

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace Amazon.Lambda.TestTool;
22

33
public abstract class Constants
44
{
5+
public const string ToolName = "lambda-test-tool";
56
public const int DefaultPort = 5050;
67
public const string DefaultHost = "localhost";
78

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ public static class ServiceCollectionExtensions
88
public static void AddCustomServices(this IServiceCollection serviceCollection,
99
ServiceLifetime lifetime = ServiceLifetime.Singleton)
1010
{
11-
serviceCollection.TryAdd(new ServiceDescriptor(typeof(ICommandFactory), typeof(CommandFactory), lifetime));
1211
serviceCollection.TryAdd(new ServiceDescriptor(typeof(IToolInteractiveService), typeof(ConsoleInteractiveService), lifetime));
13-
14-
serviceCollection.AddSingleton<AppRunner>();
12+
serviceCollection.TryAdd(new ServiceDescriptor(typeof(IDirectoryManager), typeof(DirectoryManager), lifetime));
1513
}
1614
}

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Models/ApplicationOptions.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Models/EventContainer.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class EventContainer
66
{
77
public enum Status { Queued, Executing, Success, Failure }
88

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

3131
private readonly RuntimeApiDataStore _dataStore;
32+
3233
public EventContainer(RuntimeApiDataStore dataStore, int eventCount, string eventJson)
3334
{
3435
LastUpdated = DateTime.Now;
35-
this._dataStore = dataStore;
36-
this.AwsRequestId = eventCount.ToString("D12");
37-
this.EventJson = eventJson;
36+
_dataStore = dataStore;
37+
AwsRequestId = eventCount.ToString("D12");
38+
EventJson = eventJson;
3839
}
3940

4041
public string FunctionArn
4142
{
42-
get => defaultFunctionArn;
43+
get => DefaultFunctionArn;
4344
}
4445

4546
public void ReportSuccessResponse(string response)

0 commit comments

Comments
 (0)