Skip to content

Commit bea420c

Browse files
authored
Update argument parsing and make namespace name consistent (#12)
1 parent f8ed376 commit bea420c

File tree

5 files changed

+42
-58
lines changed

5 files changed

+42
-58
lines changed

src/Azure.Functions.PowerShell.Worker.Messaging/FunctionMessagingClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using Grpc.Core;
1111
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
1212

13-
namespace Azure.Functions.PowerShell.Worker.Messaging
13+
namespace Microsoft.Azure.Functions.PowerShellWorker.Messaging
1414
{
1515
public class FunctionMessagingClient : IDisposable
1616
{

src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
14-
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.0.4" />
14+
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.1.0-rc.1" />
1515
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
16+
<PackageReference Include="CommandLineParser" Version="2.3.0" />
1617
</ItemGroup>
1718

1819
<ItemGroup>

src/Azure.Functions.PowerShell.Worker/StartupArguments.cs

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

src/Azure.Functions.PowerShell.Worker/Utility/RpcLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
using System;
77

8-
using Azure.Functions.PowerShell.Worker.Messaging;
8+
using Microsoft.Azure.Functions.PowerShellWorker.Messaging;
99
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
1010
using Microsoft.Extensions.Logging;
1111

src/Azure.Functions.PowerShell.Worker/Worker.cs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
using System;
77
using System.Threading.Tasks;
8+
using System.Management.Automation;
89
using System.Management.Automation.Runspaces;
910

10-
using Azure.Functions.PowerShell.Worker.Messaging;
11+
using CommandLine;
12+
using Microsoft.Azure.Functions.PowerShellWorker.Messaging;
1113
using Microsoft.Azure.Functions.PowerShellWorker.PowerShell.Host;
1214
using Microsoft.Azure.Functions.PowerShellWorker.Requests;
1315
using Microsoft.Azure.Functions.PowerShellWorker.Utility;
@@ -29,12 +31,17 @@ static void InitPowerShell()
2931

3032
s_runspace = RunspaceFactory.CreateRunspace(host);
3133
s_runspace.Open();
32-
s_ps = System.Management.Automation.PowerShell.Create(InitialSessionState.CreateDefault());
34+
s_ps = System.Management.Automation.PowerShell.Create();
3335
s_ps.Runspace = s_runspace;
3436

35-
s_ps.AddScript("$PSHOME");
36-
//s_ps.AddCommand("Set-ExecutionPolicy").AddParameter("ExecutionPolicy", ExecutionPolicy.Unrestricted).AddParameter("Scope", ExecutionPolicyScope.Process);
37-
s_ps.Invoke<string>();
37+
if (Platform.IsWindows)
38+
{
39+
s_ps.AddCommand("Set-ExecutionPolicy")
40+
.AddParameter("ExecutionPolicy", "Unrestricted")
41+
.AddParameter("Scope", "Process")
42+
.Invoke();
43+
s_ps.Commands.Clear();
44+
}
3845

3946
// Add HttpResponseContext namespace so users can reference
4047
// HttpResponseContext without needing to specify the full namespace
@@ -44,21 +51,23 @@ static void InitPowerShell()
4451

4552
public async static Task Main(string[] args)
4653
{
47-
StartupArguments startupArguments = StartupArguments.Parse(args);
54+
WorkerArguments arguments = null;
55+
Parser.Default.ParseArguments<WorkerArguments>(args)
56+
.WithParsed(ops => arguments = ops)
57+
.WithNotParsed(err => Environment.Exit(1));
4858

4959
// Initialize Rpc client, logger, and PowerShell
50-
s_client = new FunctionMessagingClient(startupArguments.Host, startupArguments.Port);
60+
s_client = new FunctionMessagingClient(arguments.Host, arguments.Port);
5161
s_logger = new RpcLogger(s_client);
5262
InitPowerShell();
5363

5464
// Send StartStream message
5565
var streamingMessage = new StreamingMessage() {
56-
RequestId = startupArguments.RequestId,
57-
StartStream = new StartStream() { WorkerId = startupArguments.WorkerId }
66+
RequestId = arguments.RequestId,
67+
StartStream = new StartStream() { WorkerId = arguments.WorkerId }
5868
};
5969

6070
await s_client.WriteAsync(streamingMessage);
61-
6271
await ProcessEvent();
6372
}
6473

@@ -105,4 +114,22 @@ static async Task ProcessEvent()
105114
}
106115
}
107116
}
108-
}
117+
118+
internal class WorkerArguments
119+
{
120+
[Option("host", Required = true, HelpText = "IP Address used to connect to the Host via gRPC.")]
121+
public string Host { get; set; }
122+
123+
[Option("port", Required = true, HelpText = "Port used to connect to the Host via gRPC.")]
124+
public int Port { get; set; }
125+
126+
[Option("workerId", Required = true, HelpText = "Worker ID assigned to this language worker.")]
127+
public string WorkerId { get; set; }
128+
129+
[Option("requestId", Required = true, HelpText = "Request ID used for gRPC communication with the Host.")]
130+
public string RequestId { get; set; }
131+
132+
[Option("grpcMaxMessageLength", Required = true, HelpText = "gRPC Maximum message size.")]
133+
public int MaxMessageLength { get; set; }
134+
}
135+
}

0 commit comments

Comments
 (0)