- Notifications
You must be signed in to change notification settings - Fork 488
/
Copy pathRunCommand.cs
163 lines (144 loc) · 6.56 KB
/
RunCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
usingSystem.Diagnostics;
usingSystem.Text.Json;
usingAmazon.Lambda.TestTool.Commands.Settings;
usingAmazon.Lambda.TestTool.Extensions;
usingAmazon.Lambda.TestTool.Models;
usingAmazon.Lambda.TestTool.Processes;
usingAmazon.Lambda.TestTool.Processes.SQSEventSource;
usingAmazon.Lambda.TestTool.Services;
usingAmazon.Lambda.TestTool.Services.IO;
usingSpectre.Console.Cli;
namespaceAmazon.Lambda.TestTool.Commands;
/// <summary>
/// The default command of the application which is responsible for launching the Lambda Runtime API and the API Gateway Emulator.
/// </summary>
publicsealedclassRunCommand(
IToolInteractiveServicetoolInteractiveService,IEnvironmentManagerenvironmentManager):CancellableAsyncCommand<RunCommandSettings>
{
publicconststringLAMBDA_RUNTIME_API_PORT="LAMBDA_RUNTIME_API_PORT";
publicconststringAPI_GATEWAY_EMULATOR_PORT="API_GATEWAY_EMULATOR_PORT";
/// <summary>
/// Task for the Lambda Runtime API.
/// </summary>
publicTaskLambdRuntimeApiTask{get;privateset;}
/// <summary>
/// The method responsible for executing the <see cref="RunCommand"/>.
/// </summary>
publicoverrideasyncTask<int>ExecuteAsync(CommandContextcontext,RunCommandSettingssettings,CancellationTokenSourcecancellationTokenSource)
{
try
{
EvaluateEnvironmentVariables(settings);
if(!settings.LambdaEmulatorPort.HasValue&&!settings.ApiGatewayEmulatorPort.HasValue&&string.IsNullOrEmpty(settings.SQSEventSourceConfig))
{
thrownewArgumentException("At least one of the following parameters must be set: "+
"--lambda-emulator-port, --api-gateway-emulator-port or --sqs-eventsource-config");
}
vartasks=newList<Task>();
if(settings.LambdaEmulatorPort.HasValue)
{
vartestToolProcess=TestToolProcess.Startup(settings,cancellationTokenSource.Token);
LambdRuntimeApiTask=testToolProcess.RunningTask;
tasks.Add(testToolProcess.RunningTask);
if(!settings.NoLaunchWindow)
{
try
{
varinfo=newProcessStartInfo
{
UseShellExecute=true,
FileName=testToolProcess.ServiceUrl
};
Process.Start(info);
}
catch(Exceptione)
{
toolInteractiveService.WriteErrorLine($"Error launching browser: {e.Message}");
}
}
}
if(settings.ApiGatewayEmulatorPort.HasValue)
{
if(settings.ApiGatewayEmulatorModeisnull)
{
thrownewArgumentException("When --api-gateway-emulator-port is set the --api-gateway-emulator-mode must be set to configure the mode for the API Gateway emulator.");
}
varapiGatewayEmulatorProcess=
ApiGatewayEmulatorProcess.Startup(settings,cancellationTokenSource.Token);
tasks.Add(apiGatewayEmulatorProcess.RunningTask);
}
if(!string.IsNullOrEmpty(settings.SQSEventSourceConfig))
{
varsqsEventSourceProcess=SQSEventSourceProcess.Startup(settings,cancellationTokenSource.Token);
tasks.Add(sqsEventSourceProcess.RunningTask);
}
awaitTask.Run(()=>Task.WaitAny(tasks.ToArray(),cancellationTokenSource.Token));
returnCommandReturnCodes.Success;
}
catch(OperationCanceledException)when(cancellationTokenSource.IsCancellationRequested)
{
returnCommandReturnCodes.Success;
}
catch(Exceptione)when(e.IsExpectedException())
{
toolInteractiveService.WriteErrorLine(string.Empty);
toolInteractiveService.WriteErrorLine(e.Message);
returnCommandReturnCodes.UserError;
}
catch(Exceptione)
{
// 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());
returnCommandReturnCodes.UnhandledException;
}
finally
{
awaitcancellationTokenSource.CancelAsync();
}
}
privatevoidEvaluateEnvironmentVariables(RunCommandSettingssettings)
{
varenvironmentVariables=environmentManager.GetEnvironmentVariables();
if(environmentVariables==null)
return;
if(environmentVariables.Contains(LAMBDA_RUNTIME_API_PORT))
{
varenvValue=environmentVariables[LAMBDA_RUNTIME_API_PORT]?.ToString();
if(int.TryParse(envValue,outvarport))
{
settings.LambdaEmulatorPort=port;
}
else
{
thrownewArgumentException($"Value for {LAMBDA_RUNTIME_API_PORT} environment variable was not a valid port number");
}
}
if(environmentVariables.Contains(API_GATEWAY_EMULATOR_PORT))
{
varenvValue=environmentVariables[API_GATEWAY_EMULATOR_PORT]?.ToString();
if(int.TryParse(envValue,outvarport))
{
settings.ApiGatewayEmulatorPort=port;
}
else
{
thrownewArgumentException($"Value for {API_GATEWAY_EMULATOR_PORT} environment variable was not a valid port number");
}
}
if(settings.SQSEventSourceConfig!=null&&settings.SQSEventSourceConfig.StartsWith(Constants.ArgumentEnvironmentVariablePrefix,StringComparison.CurrentCultureIgnoreCase))
{
varenvVariable=settings.SQSEventSourceConfig.Substring(Constants.ArgumentEnvironmentVariablePrefix.Length);
if(!environmentVariables.Contains(envVariable))
{
thrownewInvalidOperationException($"Environment variable {envVariable} for the SQS event source config was empty");
}
settings.SQSEventSourceConfig=environmentVariables[envVariable]?.ToString();
}
}
}