Skip to content

Commit

Permalink
Merge pull request #1898 from aws/asmarp/use-system-commandline
Browse files Browse the repository at this point in the history
feat: use System.CommandLine to create a CLI application
  • Loading branch information
philasmar authored Dec 6, 2024
2 parents 4df131f + a3a10c8 commit 2f94dbc
Show file tree
Hide file tree
Showing 23 changed files with 286 additions and 205 deletions.
8 changes: 8 additions & 0 deletions Tools/LambdaTestTool-v2/AWS.Lambda.TestTool.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Solution>
<Folder Name="/src/">
<Project Path="src\Amazon.Lambda.TestTool\Amazon.Lambda.TestTool.csproj" Type="Classic C#" />
</Folder>
<Folder Name="/tests/">
<Project Path="tests\Amazon.Lambda.TestTool.UnitTests\Amazon.Lambda.TestTool.UnitTests.csproj" Type="Classic C#" />
</Folder>
</Solution>
36 changes: 0 additions & 36 deletions Tools/LambdaTestTool-v2/aws-lambda-test-tool-v2.sln

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Blazored.Modal" Version="7.3.1" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="8.0.11" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="Blazored.Modal" Version="7.3.1" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="8.0.11" />
</ItemGroup>

<ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/AppRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.CommandLine;
using System.Text;

namespace Amazon.Lambda.TestTool;

public class AppRunner(
ICommandFactory commandFactory)
{
public async Task<int> Run(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;

return await commandFactory.BuildRootCommand().InvokeAsync(args);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.CommandLine;
using System.Diagnostics;
using Amazon.Lambda.TestTool.Extensions;
using Amazon.Lambda.TestTool.Models;
using Amazon.Lambda.TestTool.Processes;
using Amazon.Lambda.TestTool.Services;

namespace Amazon.Lambda.TestTool;

public interface ICommandFactory
{
Command BuildRootCommand();
}

public class CommandFactory(
IToolInteractiveService toolInteractiveService) : ICommandFactory
{
private static readonly object RootCommandLock = new();

public Command BuildRootCommand()
{
// Name is important to set here to show correctly in the CLI usage help.
var rootCommand = new RootCommand
{
Name = "lambda-test-tool",
Description = Constants.ProductName,
};

Option<string> hostOption = new("--host", () => Constants.DefaultHost, "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.");
Option<int> portOption = new("--port", () => Constants.DefaultPort,"The port number used for the test tool's web interface.");
Option<bool> noLaunchWindowOption = new("--no-launch-window","Disable auto launching the test tool's web interface in a browser.");
Option<bool> pauseExitOption = new("--pause-exit",() => true, "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.");
Option<bool> disableLogsOption = new("--disable-logs",() => false);

lock (RootCommandLock)
{
rootCommand.Add(hostOption);
rootCommand.Add(portOption);
rootCommand.Add(noLaunchWindowOption);
rootCommand.Add(pauseExitOption);
rootCommand.Add(disableLogsOption);
}

rootCommand.SetHandler(async (context) =>
{
try
{
var lambdaOptions = new ApplicationOptions
{
Host = context.ParseResult.GetValueForOption(hostOption) ?? Constants.DefaultHost,
Port = context.ParseResult.GetValueForOption(portOption),
NoLaunchWindow = context.ParseResult.GetValueForOption(noLaunchWindowOption),
PauseExit = context.ParseResult.GetValueForOption(pauseExitOption),
DisableLogs = context.ParseResult.GetValueForOption(disableLogsOption)
};

var process = TestToolProcess.Startup(lambdaOptions);

if (!lambdaOptions.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;

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

context.ExitCode = 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());

context.ExitCode = CommandReturnCodes.UnhandledException;
}
});

return rootCommand;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@inherits LayoutComponentBase
@using Amazon.Lambda.TestTool.Utilities
@inherits LayoutComponentBase

<div class="page">
<div class="sidebar">
Expand All @@ -10,7 +11,7 @@
@Body
<hr />
<footer>
<p>@Constants.PRODUCT_NAME (@Utils.DetermineToolVersion()) is an open source project. For issues or feedback visit the <a href="@Constants.LINK_GITHUB_TEST_TOOL">AWS Lambda for .NET Core GitHub repository.</a></p>
<p>@Constants.ProductName (@Utils.DetermineToolVersion()) is an open source project. For issues or feedback visit the <a href="@Constants.LinkGithubTestTool">AWS Lambda for .NET Core GitHub repository.</a></p>
</footer>
</article>
</main>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
@using Microsoft.AspNetCore.Http;
@inject IHttpContextAccessor httpContextAccessor;

<PageTitle>@Constants.PRODUCT_NAME</PageTitle>
<PageTitle>@Constants.ProductName</PageTitle>

<h1 style="padding: 15px">@Constants.PRODUCT_NAME</h1>
<h1 style="padding: 15px">@Constants.ProductName</h1>
<hr />

<p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
@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 LambdaTestToolOptions LambdaOptions
@inject ApplicationOptions LambdaOptions
@inject IRuntimeApiDataStore RuntimeApiModel

<PageTitle>Lambd Function Tester</PageTitle>
Expand Down Expand Up @@ -81,7 +82,7 @@
{
<p>
<b>Response:</b>
<pre class="form-control" style="@Constants.ResponseSuccessStyleSizeConstraint">@Amazon.Lambda.TestTool.Utils.TryPrettyPrintJson(RuntimeApiModel.ActiveEvent.Response)</pre>
<pre class="form-control" style="@Constants.ResponseSuccessStyleSizeConstraint">@Utils.TryPrettyPrintJson(RuntimeApiModel.ActiveEvent.Response)</pre>
</p>
}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@using Amazon.Lambda.TestTool.SampleRequests;
@inject LambdaTestToolOptions LambdaOptions
@using Amazon.Lambda.TestTool.Models
@using Amazon.Lambda.TestTool.SampleRequests;
@inject ApplicationOptions LambdaOptions
@inject IModalService ModalService

<div class="simple-form" style="min-width: 400px">
Expand Down
20 changes: 10 additions & 10 deletions Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
namespace Amazon.Lambda.TestTool;

public class Constants
public abstract class Constants
{
public const int DEFAULT_PORT = 5050;
public const string DEFAULT_HOST = "localhost";
public const int DefaultPort = 5050;
public const string DefaultHost = "localhost";

public const string PRODUCT_NAME = "AWS .NET Mock Lambda Test Tool";
public const string ProductName = "AWS .NET Mock Lambda Test Tool";

public const string ResponseSuccessStyle = "white-space: pre-wrap; height: min-content; font-size: 75%; color: black";
public const string ResponseErrorStyle = "white-space: pre-wrap; height: min-content; font-size: 75%; color: red";

public const string ResponseSuccessStyleSizeConstraint = "white-space: pre-wrap; height: 300px; font-size: 75%; color: black";
public const string ResponseErrorStyleSizeConstraint = "white-space: pre-wrap; height: 300px; font-size: 75%; color: red";


public const string LINK_GITHUB_TEST_TOOL = "https://github.com/aws/aws-lambda-dotnet/tree/master/Tools/LambdaTestTool";
public const string LINK_GITHUB_TEST_TOOL_INSTALL_AND_RUN = "https://github.com/aws/aws-lambda-dotnet/tree/master/Tools/LambdaTestTool#installing-and-running";
public const string LINK_DLQ_DEVELOEPR_GUIDE = "https://docs.aws.amazon.com/lambda/latest/dg/dlq.html";
public const string LINK_MSDN_ASSEMBLY_LOAD_CONTEXT = "https://docs.microsoft.com/en-us/dotnet/api/system.runtime.loader.assemblyloadcontext";
public const string LINK_VS_TOOLKIT_MARKETPLACE = "https://marketplace.visualstudio.com/items?itemName=AmazonWebServices.AWSToolkitforVisualStudio2017";
public const string LinkGithubRepo = "https://github.com/aws/aws-lambda-dotnet";
public const string LinkGithubTestTool = "https://github.com/aws/aws-lambda-dotnet/tree/master/Tools/LambdaTestTool";
public const string LinkGithubTestToolInstallAndRun = "https://github.com/aws/aws-lambda-dotnet/tree/master/Tools/LambdaTestTool#installing-and-running";
public const string LinkDlqDeveloperGuide = "https://docs.aws.amazon.com/lambda/latest/dg/dlq.html";
public const string LinkMsdnAssemblyLoadContext = "https://docs.microsoft.com/en-us/dotnet/api/system.runtime.loader.assemblyloadcontext";
public const string LinkVsToolkitMarketplace = "https://marketplace.visualstudio.com/items?itemName=AmazonWebServices.AWSToolkitforVisualStudio2017";
}
Loading

0 comments on commit 2f94dbc

Please sign in to comment.