Skip to content

Commit 93464f4

Browse files
committed
Bumps up System.Commandline API to v2.0.0-beta4.22272.1 and resolve breaking change
1 parent 521920d commit 93464f4

File tree

4 files changed

+152
-12
lines changed

4 files changed

+152
-12
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.CommandLine;
6+
using System.CommandLine.Invocation;
7+
using System.IO;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
using Microsoft.Extensions.Logging;
11+
12+
namespace Microsoft.OpenApi.Hidi.Handlers
13+
{
14+
internal class TransformCommandHandler : ICommandHandler
15+
{
16+
public Option<string> DescriptionOption { get; set; }
17+
public Option<string> CsdlOption { get; set; }
18+
public Option<string> CsdlFilterOption { get; set; }
19+
public Option<FileInfo> OutputOption { get; set; }
20+
public Option<bool> CleanOutputOption { get; set; }
21+
public Option<string?> VersionOption { get; set; }
22+
public Option<OpenApiFormat?> FormatOption { get; set; }
23+
public Option<bool> TerseOutputOption { get; set; }
24+
public Option<LogLevel> LogLevelOption { get; set; }
25+
public Option<string> FilterByOperationIdsOption { get; set; }
26+
public Option<string> FilterByTagsOption { get; set; }
27+
public Option<string> FilterByCollectionOption { get; set; }
28+
public Option<bool> InlineLocalOption { get; set; }
29+
public Option<bool> InlineExternalOption { get; set; }
30+
31+
public int Invoke(InvocationContext context)
32+
{
33+
return InvokeAsync(context).GetAwaiter().GetResult();
34+
}
35+
public async Task<int> InvokeAsync(InvocationContext context)
36+
{
37+
string openapi = context.ParseResult.GetValueForOption(DescriptionOption);
38+
string csdlFilter = context.ParseResult.GetValueForOption(CsdlFilterOption);
39+
string csdl = context.ParseResult.GetValueForOption(CsdlOption);
40+
FileInfo output = context.ParseResult.GetValueForOption(OutputOption);
41+
bool cleanOutput = context.ParseResult.GetValueForOption(CleanOutputOption);
42+
string? version = context.ParseResult.GetValueForOption(VersionOption);
43+
OpenApiFormat? format = context.ParseResult.GetValueForOption(FormatOption);
44+
bool terseOutput = context.ParseResult.GetValueForOption(TerseOutputOption);
45+
LogLevel logLevel = context.ParseResult.GetValueForOption(LogLevelOption);
46+
bool inlineLocal = context.ParseResult.GetValueForOption(InlineLocalOption);
47+
bool inlineExternal = context.ParseResult.GetValueForOption(InlineExternalOption);
48+
string filterbyoperationids = context.ParseResult.GetValueForOption(FilterByOperationIdsOption);
49+
string filterbytags = context.ParseResult.GetValueForOption(FilterByTagsOption);
50+
string filterbycollection = context.ParseResult.GetValueForOption(FilterByCollectionOption);
51+
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));
52+
53+
var logger = Logger.ConfigureLogger(logLevel);
54+
55+
try
56+
{
57+
await OpenApiService.TransformOpenApiDocument(openapi, csdl, csdlFilter, output, cleanOutput, version, format, terseOutput, logLevel, inlineLocal, inlineExternal, filterbyoperationids, filterbytags, filterbycollection, cancellationToken);
58+
59+
return 0;
60+
}
61+
catch (Exception ex)
62+
{
63+
#if DEBUG
64+
logger.LogCritical(ex, ex.Message);
65+
throw; // so debug tools go straight to the source of the exception when attached
66+
#else
67+
logger.LogCritical( ex.Message);
68+
return 1;
69+
#endif
70+
}
71+
}
72+
}
73+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.CommandLine;
6+
using System.CommandLine.Invocation;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace Microsoft.OpenApi.Hidi.Handlers
12+
{
13+
internal class ValidateCommandHandler : ICommandHandler
14+
{
15+
public Option<string> DescriptionOption { get; set; }
16+
public Option<LogLevel> LogLevelOption { get; set; }
17+
18+
public int Invoke(InvocationContext context)
19+
{
20+
return InvokeAsync(context).GetAwaiter().GetResult();
21+
}
22+
public async Task<int> InvokeAsync(InvocationContext context)
23+
{
24+
string openapi = context.ParseResult.GetValueForOption(DescriptionOption);
25+
LogLevel logLevel = context.ParseResult.GetValueForOption(LogLevelOption);
26+
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));
27+
28+
29+
var logger = Logger.ConfigureLogger(logLevel);
30+
31+
try
32+
{
33+
await OpenApiService.ValidateOpenApiDocument(openapi, logLevel, cancellationToken);
34+
return 0;
35+
}
36+
catch (Exception ex)
37+
{
38+
#if DEBUG
39+
logger.LogCritical(ex, ex.Message);
40+
throw; // so debug tools go straight to the source of the exception when attached
41+
#else
42+
logger.LogCritical( ex.Message);
43+
Environment.Exit(1);
44+
return 1;
45+
#endif
46+
}
47+
}
48+
}
49+
}

src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@
3737
</ItemGroup>
3838

3939
<ItemGroup>
40-
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
41-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
42-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
43-
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
44-
<PackageReference Include="System.CommandLine" Version="2.0.0-beta3.22114.1" />
45-
<PackageReference Include="Microsoft.OData.Edm" Version="7.12.0" />
46-
<PackageReference Include="Microsoft.OpenApi.OData" Version="1.0.11-preview4" />
40+
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0-preview.6.22324.4" />
41+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0-preview.6.22324.4" />
42+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0-preview.6.22324.4" />
43+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0-preview.6.22324.4" />
44+
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
45+
<PackageReference Include="Microsoft.OData.Edm" Version="7.12.1" />
46+
<PackageReference Include="Microsoft.OpenApi.OData" Version="1.0.11" />
4747
</ItemGroup>
4848

4949
<ItemGroup>

src/Microsoft.OpenApi.Hidi/Program.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System;
54
using System.CommandLine;
65
using System.IO;
7-
using System.Threading;
86
using System.Threading.Tasks;
97
using Microsoft.Extensions.Logging;
8+
using Microsoft.OpenApi.Hidi.Handlers;
109

1110
namespace Microsoft.OpenApi.Hidi
1211
{
@@ -66,7 +65,11 @@ static async Task Main(string[] args)
6665
logLevelOption
6766
};
6867

69-
validateCommand.SetHandler<string, LogLevel, CancellationToken>(OpenApiService.ValidateOpenApiDocument, descriptionOption, logLevelOption);
68+
validateCommand.Handler = new ValidateCommandHandler
69+
{
70+
DescriptionOption = descriptionOption,
71+
LogLevelOption = logLevelOption
72+
};
7073

7174
var transformCommand = new Command("transform")
7275
{
@@ -86,8 +89,23 @@ static async Task Main(string[] args)
8689
inlineExternalOption
8790
};
8891

89-
transformCommand.SetHandler<string, string, string, FileInfo, bool, string?, OpenApiFormat?, bool, LogLevel, bool, bool, string, string, string, CancellationToken> (
90-
OpenApiService.TransformOpenApiDocument, descriptionOption, csdlOption, csdlFilterOption, outputOption, cleanOutputOption, versionOption, formatOption, terseOutputOption, logLevelOption, inlineLocalOption, inlineExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption);
92+
transformCommand.Handler = new TransformCommandHandler
93+
{
94+
DescriptionOption = descriptionOption,
95+
CsdlOption = csdlOption,
96+
CsdlFilterOption = csdlFilterOption,
97+
OutputOption = outputOption,
98+
CleanOutputOption = cleanOutputOption,
99+
VersionOption = versionOption,
100+
FormatOption = formatOption,
101+
TerseOutputOption = terseOutputOption,
102+
LogLevelOption = logLevelOption,
103+
FilterByOperationIdsOption = filterByOperationIdsOption,
104+
FilterByTagsOption = filterByTagsOption,
105+
FilterByCollectionOption = filterByCollectionOption,
106+
InlineLocalOption = inlineLocalOption,
107+
InlineExternalOption = inlineExternalOption
108+
};
91109

92110
rootCommand.Add(transformCommand);
93111
rootCommand.Add(validateCommand);

0 commit comments

Comments
 (0)