-
Notifications
You must be signed in to change notification settings - Fork 242
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
93464f4
Bumps up System.Commandline API to v2.0.0-beta4.22272.1 and resolve b…
MaggieKimani1 8a91397
Add logger class for easy reuse and clean up code
MaggieKimani1 9e51365
Merge branch 'vnext' into mk/upgrade-system-commandline
MaggieKimani1 a3b4b0d
Clean up code
MaggieKimani1 02570f5
Refactor code
MaggieKimani1 f0a0eb8
Revert packages to stable versions
MaggieKimani1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
src/Microsoft.OpenApi.Hidi/Handlers/TransformCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Microsoft.OpenApi.Hidi/Handlers/ValidateCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Generic catch clause