Skip to content

Commit 1b3588c

Browse files
authored
Update interop client to use System.Commandline (#890)
1 parent 97af9b2 commit 1b3588c

File tree

4 files changed

+44
-87
lines changed

4 files changed

+44
-87
lines changed

build/dependencies.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<Project>
22
<PropertyGroup>
33
<BenchmarkDotNetPackageVersion>0.11.3</BenchmarkDotNetPackageVersion>
4-
<CommandLineParserPackageVersion>2.3.0</CommandLineParserPackageVersion>
54
<GoogleProtobufPackageVersion>3.11.4</GoogleProtobufPackageVersion>
65
<GrpcDotNetPackageVersion>2.28.0-pre2</GrpcDotNetPackageVersion> <!-- Only use for example projects -->
76
<GrpcPackageVersion>2.27.0</GrpcPackageVersion>

testassets/InteropTestsClient/InteropTestsClient.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
<ProjectReference Include="..\..\src\Grpc.Net.Client\Grpc.Net.Client.csproj" />
2828

29-
<PackageReference Include="CommandLineParser" Version="$(CommandLineParserPackageVersion)" />
3029
<PackageReference Include="Google.Protobuf" Version="$(GoogleProtobufPackageVersion)" />
3130
<PackageReference Include="Grpc.Auth" Version="$(GrpcPackageVersion)" PrivateAssets="All" />
3231
<PackageReference Include="Grpc.Core" Version="$(GrpcPackageVersion)" PrivateAssets="All" />
@@ -35,6 +34,7 @@
3534
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsPackageVersion)" />
3635
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(MicrosoftExtensionsPackageVersion)" />
3736
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPackageVersion)" />
37+
<PackageReference Include="System.CommandLine" Version="$(SystemCommandLinePackageVersion)" />
3838
</ItemGroup>
3939

4040
</Project>

testassets/InteropTestsClient/Program.cs

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
#endregion
1818

1919
using System;
20-
using CommandLine;
21-
using Grpc.Core;
22-
using Grpc.Core.Logging;
20+
using System.CommandLine;
21+
using System.CommandLine.Invocation;
22+
using System.Threading.Tasks;
2323
using Grpc.Shared.TestAssets;
2424
using Microsoft.Extensions.DependencyInjection;
2525
using Microsoft.Extensions.Logging;
@@ -28,31 +28,43 @@ namespace InteropTestsClient
2828
{
2929
public class Program
3030
{
31-
public static void Main(string[] args)
31+
public static async Task<int> Main(string[] args)
3232
{
33-
GrpcEnvironment.SetLogger(new ConsoleLogger());
34-
var parserResult = Parser.Default.ParseArguments<ClientOptions>(args)
35-
.WithNotParsed(errors => Environment.Exit(1))
36-
.WithParsed(options =>
33+
var rootCommand = new RootCommand();
34+
rootCommand.AddOption(new Option<string>(new string[] { "--client_type", nameof(ClientOptions.ClientType) }, () => "httpclient"));
35+
rootCommand.AddOption(new Option<string>(new string[] { "--server_host", nameof(ClientOptions.ServerHost) }) { Required = true });
36+
rootCommand.AddOption(new Option<string>(new string[] { "--server_host_override", nameof(ClientOptions.ServerHostOverride) }));
37+
rootCommand.AddOption(new Option<int>(new string[] { "--server_port", nameof(ClientOptions.ServerPort) }) { Required = true });
38+
rootCommand.AddOption(new Option<string>(new string[] { "--test_case", nameof(ClientOptions.TestCase) }) { Required = true });
39+
rootCommand.AddOption(new Option<bool>(new string[] { "--use_tls", nameof(ClientOptions.UseTls) }));
40+
rootCommand.AddOption(new Option<bool>(new string[] { "--use_test_ca", nameof(ClientOptions.UseTestCa) }));
41+
rootCommand.AddOption(new Option<string>(new string[] { "--default_service_account", nameof(ClientOptions.DefaultServiceAccount) }));
42+
rootCommand.AddOption(new Option<string>(new string[] { "--oauth_scope", nameof(ClientOptions.OAuthScope) }));
43+
rootCommand.AddOption(new Option<string>(new string[] { "--service_account_key_file", nameof(ClientOptions.ServiceAccountKeyFile) }));
44+
rootCommand.AddOption(new Option<string>(new string[] { "--grpc_web_mode", nameof(ClientOptions.GrpcWebMode) }));
45+
46+
rootCommand.Handler = CommandHandler.Create<ClientOptions>(async (options) =>
47+
{
48+
Console.WriteLine("Use TLS: " + options.UseTls);
49+
Console.WriteLine("Use Test CA: " + options.UseTestCa);
50+
Console.WriteLine("Client type: " + options.ClientType);
51+
Console.WriteLine("Server host: " + options.ServerHost);
52+
Console.WriteLine("Server port: " + options.ServerPort);
53+
54+
var services = new ServiceCollection();
55+
services.AddLogging(configure =>
3756
{
38-
Console.WriteLine("Use TLS: " + options.UseTls);
39-
Console.WriteLine("Use Test CA: " + options.UseTestCa);
40-
Console.WriteLine("Client type: " + options.ClientType);
41-
Console.WriteLine("Server host: " + options.ServerHost);
42-
Console.WriteLine("Server port: " + options.ServerPort);
43-
44-
var services = new ServiceCollection();
45-
services.AddLogging(configure =>
46-
{
47-
configure.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
48-
configure.AddConsole(loggerOptions => loggerOptions.IncludeScopes = true);
49-
});
50-
51-
using var serviceProvider = services.BuildServiceProvider();
52-
53-
var interopClient = new InteropClient(options, serviceProvider.GetRequiredService<ILoggerFactory>());
54-
interopClient.Run().Wait();
57+
configure.SetMinimumLevel(LogLevel.Trace);
58+
configure.AddConsole(loggerOptions => loggerOptions.IncludeScopes = true);
5559
});
60+
61+
using var serviceProvider = services.BuildServiceProvider();
62+
63+
var interopClient = new InteropClient(options, serviceProvider.GetRequiredService<ILoggerFactory>());
64+
await interopClient.Run();
65+
});
66+
67+
return await rootCommand.InvokeAsync(args);
5668
}
5769
}
5870
}

testassets/Shared/InteropClient.cs

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
using Grpc.Shared.TestAssets;
3535

3636
#if !NETSTANDARD2_1
37-
using CommandLine;
3837
using Google.Apis.Auth.OAuth2;
3938
using Grpc.Auth;
4039
using Grpc.Core.Logging;
@@ -46,69 +45,16 @@ namespace Grpc.Shared.TestAssets
4645
{
4746
public class ClientOptions
4847
{
49-
#if !NETSTANDARD2_1
50-
[Option("client_type", Default = "httpclient")]
51-
#endif
5248
public string? ClientType { get; set; }
53-
54-
#if !NETSTANDARD2_1
55-
[Option("server_host", Default = "localhost")]
56-
#endif
5749
public string? ServerHost { get; set; }
58-
59-
#if !NETSTANDARD2_1
60-
[Option("server_host_override")]
61-
#endif
6250
public string? ServerHostOverride { get; set; }
63-
64-
#if !NETSTANDARD2_1
65-
[Option("server_port"
66-
#if DEBUG
67-
, Default = 50052
68-
#endif
69-
)]
70-
#endif
7151
public int ServerPort { get; set; }
72-
73-
#if !NETSTANDARD2_1
74-
[Option("test_case"
75-
#if DEBUG
76-
, Default = "large_unary"
77-
#endif
78-
)]
79-
#endif
8052
public string? TestCase { get; set; }
81-
82-
// Deliberately using nullable bool type to allow --use_tls=true syntax (as opposed to --use_tls)
83-
#if !NETSTANDARD2_1
84-
[Option("use_tls", Default = false)]
85-
#endif
86-
public bool? UseTls { get; set; }
87-
88-
// Deliberately using nullable bool type to allow --use_test_ca=true syntax (as opposed to --use_test_ca)
89-
#if !NETSTANDARD2_1
90-
[Option("use_test_ca", Default = false)]
91-
#endif
92-
public bool? UseTestCa { get; set; }
93-
94-
#if !NETSTANDARD2_1
95-
[Option("default_service_account", Required = false)]
96-
#endif
53+
public bool UseTls { get; set; }
54+
public bool UseTestCa { get; set; }
9755
public string? DefaultServiceAccount { get; set; }
98-
99-
#if !NETSTANDARD2_1
100-
[Option("oauth_scope", Required = false)]
101-
#endif
10256
public string? OAuthScope { get; set; }
103-
104-
#if !NETSTANDARD2_1
105-
[Option("service_account_key_file", Required = false)]
106-
#endif
10757
public string? ServiceAccountKeyFile { get; set; }
108-
109-
#if !NETSTANDARD2_1
110-
[Option("grpc_web_mode")]
111-
#endif
11258
public string? GrpcWebMode { get; set; }
11359
}
11460

@@ -158,7 +104,7 @@ private async Task<IChannelWrapper> HttpClientCreateChannel()
158104
var credentials = await CreateCredentialsAsync(useTestCaOverride: false);
159105

160106
string scheme;
161-
if (!(options.UseTls ?? false))
107+
if (!options.UseTls)
162108
{
163109
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
164110
scheme = "http";
@@ -173,7 +119,7 @@ private async Task<IChannelWrapper> HttpClientCreateChannel()
173119
httpClientHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
174120
#endif
175121

176-
if (options.UseTestCa ?? false)
122+
if (options.UseTestCa)
177123
{
178124
var pem = File.ReadAllText("Certs/ca.pem");
179125
var certData = GetBytesFromPem(pem, "CERTIFICATE");
@@ -230,10 +176,10 @@ private async Task<IChannelWrapper> CoreCreateChannel()
230176
private async Task<ChannelCredentials> CreateCredentialsAsync(bool? useTestCaOverride = null)
231177
{
232178
var credentials = ChannelCredentials.Insecure;
233-
if (options.UseTls.GetValueOrDefault())
179+
if (options.UseTls)
234180
{
235181
#if !NETSTANDARD2_1
236-
if (useTestCaOverride ?? options.UseTestCa.GetValueOrDefault())
182+
if (useTestCaOverride ?? options.UseTestCa)
237183
{
238184
credentials = TestCredentials.CreateSslCredentials();
239185
}

0 commit comments

Comments
 (0)