-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1898 from aws/asmarp/use-system-commandline
feat: use System.CommandLine to create a CLI application
- Loading branch information
Showing
23 changed files
with
286 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/AppRunner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
104 changes: 0 additions & 104 deletions
104
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/CommandLineOptions.cs
This file was deleted.
Oops, something went wrong.
102 changes: 102 additions & 0 deletions
102
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Commands/CommandFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Components/Pages/SaveRequestDialog.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 10 additions & 10 deletions
20
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Constants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
Oops, something went wrong.