|
| 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 | + using var loggerFactory = Logger.ConfigureLogger(logLevel); |
| 54 | + var logger = loggerFactory.CreateLogger<OpenApiService>(); |
| 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 | +} |
0 commit comments