Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump up System.Commandline API to v2.0.0-beta4.22272.1 #959

Merged
merged 6 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/Microsoft.OpenApi.Hidi/Handlers/TransformCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace Microsoft.OpenApi.Hidi.Handlers
{
internal class TransformCommandHandler : ICommandHandler
{
public Option<string> DescriptionOption { get; set; }
public Option<string> CsdlOption { get; set; }
public Option<string> CsdlFilterOption { get; set; }
public Option<FileInfo> OutputOption { get; set; }
public Option<bool> CleanOutputOption { get; set; }
public Option<string?> VersionOption { get; set; }
public Option<OpenApiFormat?> FormatOption { get; set; }
public Option<bool> TerseOutputOption { get; set; }
public Option<LogLevel> LogLevelOption { get; set; }
public Option<string> FilterByOperationIdsOption { get; set; }
public Option<string> FilterByTagsOption { get; set; }
public Option<string> FilterByCollectionOption { get; set; }
public Option<bool> InlineLocalOption { get; set; }
public Option<bool> InlineExternalOption { get; set; }

public int Invoke(InvocationContext context)
{
return InvokeAsync(context).GetAwaiter().GetResult();
}
public async Task<int> InvokeAsync(InvocationContext context)
{
string openapi = context.ParseResult.GetValueForOption(DescriptionOption);
string csdlFilter = context.ParseResult.GetValueForOption(CsdlFilterOption);
string csdl = context.ParseResult.GetValueForOption(CsdlOption);
FileInfo output = context.ParseResult.GetValueForOption(OutputOption);
bool cleanOutput = context.ParseResult.GetValueForOption(CleanOutputOption);
string? version = context.ParseResult.GetValueForOption(VersionOption);
OpenApiFormat? format = context.ParseResult.GetValueForOption(FormatOption);
bool terseOutput = context.ParseResult.GetValueForOption(TerseOutputOption);
LogLevel logLevel = context.ParseResult.GetValueForOption(LogLevelOption);
bool inlineLocal = context.ParseResult.GetValueForOption(InlineLocalOption);
bool inlineExternal = context.ParseResult.GetValueForOption(InlineExternalOption);
string filterbyoperationids = context.ParseResult.GetValueForOption(FilterByOperationIdsOption);
string filterbytags = context.ParseResult.GetValueForOption(FilterByTagsOption);
string filterbycollection = context.ParseResult.GetValueForOption(FilterByCollectionOption);
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));

using var loggerFactory = Logger.ConfigureLogger(logLevel);
var logger = loggerFactory.CreateLogger<OpenApiService>();
try
{
await OpenApiService.TransformOpenApiDocument(openapi, csdl, csdlFilter, output, cleanOutput, version, format, terseOutput, logLevel, inlineLocal, inlineExternal, filterbyoperationids, filterbytags, filterbycollection, cancellationToken);

return 0;
}
catch (Exception ex)
{
#if DEBUG
logger.LogCritical(ex, ex.Message);
throw; // so debug tools go straight to the source of the exception when attached
#else
logger.LogCritical( ex.Message);
return 1;
#endif
}
Comment on lines +61 to +70

Check notice

Code scanning / CodeQL

Generic catch clause

Generic catch clause.
}
}
}
48 changes: 48 additions & 0 deletions src/Microsoft.OpenApi.Hidi/Handlers/ValidateCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace Microsoft.OpenApi.Hidi.Handlers
{
internal class ValidateCommandHandler : ICommandHandler
{
public Option<string> DescriptionOption { get; set; }
public Option<LogLevel> LogLevelOption { get; set; }

public int Invoke(InvocationContext context)
{
return InvokeAsync(context).GetAwaiter().GetResult();
}
public async Task<int> InvokeAsync(InvocationContext context)
{
string openapi = context.ParseResult.GetValueForOption(DescriptionOption);
LogLevel logLevel = context.ParseResult.GetValueForOption(LogLevelOption);
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));


using var loggerFactory = Logger.ConfigureLogger(logLevel);
var logger = loggerFactory.CreateLogger<OpenApiService>();
try
{
await OpenApiService.ValidateOpenApiDocument(openapi, logLevel, cancellationToken);
return 0;
}
catch (Exception ex)
{
#if DEBUG
logger.LogCritical(ex, ex.Message);
throw; // so debug tools go straight to the source of the exception when attached
#else
logger.LogCritical( ex.Message);
return 1;
#endif
}
}
}
}
31 changes: 31 additions & 0 deletions src/Microsoft.OpenApi.Hidi/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using Microsoft.Extensions.Logging;

namespace Microsoft.OpenApi.Hidi
{
public class Logger
{
public static ILoggerFactory ConfigureLogger(LogLevel logLevel)
{
// Configure logger options
#if DEBUG
logLevel = logLevel > LogLevel.Debug ? LogLevel.Debug : logLevel;
#endif

return LoggerFactory.Create((builder) =>
{
builder
.AddSimpleConsole(c =>
{
c.IncludeScopes = true;
})
#if DEBUG
.AddDebug()
#endif
.SetMinimumLevel(logLevel);
});
}
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta3.22114.1" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.12.1" />
<PackageReference Include="Microsoft.OpenApi.OData" Version="1.0.11" />
</ItemGroup>
Expand Down
40 changes: 11 additions & 29 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
Expand Down Expand Up @@ -26,7 +26,6 @@
using System.Threading;
using System.Xml.Xsl;
using System.Xml;
using System.Runtime.CompilerServices;
using System.Reflection;

namespace Microsoft.OpenApi.Hidi
Expand All @@ -36,7 +35,7 @@ public class OpenApiService
/// <summary>
/// Implementation of the transform command
/// </summary>
public static async Task<int> TransformOpenApiDocument(
public static async Task TransformOpenApiDocument(
string openapi,
string csdl,
string csdlFilter,
Expand All @@ -45,7 +44,7 @@ public static async Task<int> TransformOpenApiDocument(
string? version,
OpenApiFormat? format,
bool terseOutput,
LogLevel loglevel,
LogLevel logLevel,
bool inlineLocal,
bool inlineExternal,
string filterbyoperationids,
Expand All @@ -54,9 +53,8 @@ public static async Task<int> TransformOpenApiDocument(
CancellationToken cancellationToken
)
{
using var loggerFactory = ConfigureLoggerInstance(loglevel);
using var loggerFactory = Logger.ConfigureLogger(logLevel);
var logger = loggerFactory.CreateLogger<OpenApiService>();

try
{
if (string.IsNullOrEmpty(openapi) && string.IsNullOrEmpty(csdl))
Expand Down Expand Up @@ -213,18 +211,11 @@ CancellationToken cancellationToken
logger.LogTrace($"Finished serializing in {stopwatch.ElapsedMilliseconds}ms");
textWriter.Flush();
}
return 0;
}
catch (Exception ex)
{
#if DEBUG
logger.LogCritical(ex, ex.Message);
#else
logger.LogCritical(ex.Message);

#endif
return 1;
}
throw new InvalidOperationException($"Could not transform the document, reason: {ex.Message}", ex);
}
Comment on lines 215 to +218

Check notice

Code scanning / CodeQL

Generic catch clause

Generic catch clause.
}

private static XslCompiledTransform GetFilterTransform()
Expand Down Expand Up @@ -253,14 +244,13 @@ private static Stream ApplyFilter(string csdl, string entitySetOrSingleton, XslC
/// <summary>
/// Implementation of the validate command
/// </summary>
public static async Task<int> ValidateOpenApiDocument(
public static async Task ValidateOpenApiDocument(
string openapi,
LogLevel loglevel,
LogLevel logLevel,
CancellationToken cancellationToken)
{
using var loggerFactory = ConfigureLoggerInstance(loglevel);
using var loggerFactory = Logger.ConfigureLogger(logLevel);
var logger = loggerFactory.CreateLogger<OpenApiService>();

try
{
if (string.IsNullOrEmpty(openapi))
Expand Down Expand Up @@ -307,19 +297,11 @@ public static async Task<int> ValidateOpenApiDocument(
logger.LogTrace("Finished walking through the OpenApi document. Generating a statistics report..");
logger.LogInformation(statsVisitor.GetStatisticsReport());
}

return 0;
}
catch (Exception ex)
{
#if DEBUG
logger.LogCritical(ex, ex.Message);
#else
logger.LogCritical(ex.Message);
#endif
return 1;
throw new InvalidOperationException($"Could not validate the document, reason: {ex.Message}", ex);
}

}

/// <summary>
Expand Down Expand Up @@ -596,7 +578,7 @@ private static ILoggerFactory ConfigureLoggerInstance(LogLevel loglevel)
loglevel = loglevel > LogLevel.Debug ? LogLevel.Debug : loglevel;
#endif

return LoggerFactory.Create((builder) => {
return Microsoft.Extensions.Logging.LoggerFactory.Create((builder) => {
builder
.AddSimpleConsole(c => {
c.IncludeScopes = true;
Expand Down
28 changes: 23 additions & 5 deletions src/Microsoft.OpenApi.Hidi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.CommandLine;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Hidi.Handlers;

namespace Microsoft.OpenApi.Hidi
{
Expand Down Expand Up @@ -66,7 +65,11 @@ static async Task Main(string[] args)
logLevelOption
};

validateCommand.SetHandler<string, LogLevel, CancellationToken>(OpenApiService.ValidateOpenApiDocument, descriptionOption, logLevelOption);
validateCommand.Handler = new ValidateCommandHandler
{
DescriptionOption = descriptionOption,
LogLevelOption = logLevelOption
};

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

transformCommand.SetHandler<string, string, string, FileInfo, bool, string?, OpenApiFormat?, bool, LogLevel, bool, bool, string, string, string, CancellationToken> (
OpenApiService.TransformOpenApiDocument, descriptionOption, csdlOption, csdlFilterOption, outputOption, cleanOutputOption, versionOption, formatOption, terseOutputOption, logLevelOption, inlineLocalOption, inlineExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption);
transformCommand.Handler = new TransformCommandHandler
{
DescriptionOption = descriptionOption,
CsdlOption = csdlOption,
CsdlFilterOption = csdlFilterOption,
OutputOption = outputOption,
CleanOutputOption = cleanOutputOption,
VersionOption = versionOption,
FormatOption = formatOption,
TerseOutputOption = terseOutputOption,
LogLevelOption = logLevelOption,
FilterByOperationIdsOption = filterByOperationIdsOption,
FilterByTagsOption = filterByTagsOption,
FilterByCollectionOption = filterByCollectionOption,
InlineLocalOption = inlineLocalOption,
InlineExternalOption = inlineExternalOption
};

rootCommand.Add(transformCommand);
rootCommand.Add(validateCommand);
Expand Down