-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
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,55 @@ | ||
using System.CommandLine; | ||
using Microsoft.OpenApi; | ||
using Microsoft.OpenApi.Extensions; | ||
using OpenApiGenerator.Core.Extensions; | ||
|
||
namespace OpenApiGenerator.Cli.Commands; | ||
|
||
public class SimplifyCommand : Command | ||
{ | ||
public SimplifyCommand() : base(name: "simplify", description: "Simplifies OpenAPI spec.") | ||
{ | ||
var inputOption = new Argument<string>( | ||
name: "input", | ||
getDefaultValue: () => string.Empty, | ||
description: "Input file path"); | ||
var outputOption = new Option<string>( | ||
aliases: ["--output", "-o"], | ||
getDefaultValue: () => "simplified.yaml", | ||
description: "Output file path"); | ||
AddArgument(inputOption); | ||
AddOption(outputOption); | ||
|
||
this.SetHandler( | ||
HandleAsync, | ||
inputOption, | ||
outputOption); | ||
} | ||
|
||
private static async Task HandleAsync( | ||
string inputPath, | ||
string outputPath) | ||
{ | ||
Console.WriteLine($"Loading {inputPath}..."); | ||
|
||
using var client = new HttpClient(); | ||
var yamlOrJson = inputPath.StartsWith("http", StringComparison.OrdinalIgnoreCase) | ||
? await client.GetStringAsync(new Uri(inputPath)).ConfigureAwait(false) | ||
: await File.ReadAllTextAsync(inputPath).ConfigureAwait(false); | ||
var openApiDocument = yamlOrJson.GetOpenApiDocument(); | ||
|
||
Console.WriteLine("Simplifying..."); | ||
|
||
openApiDocument = openApiDocument.Simplify(); | ||
|
||
var text = Path.GetExtension(outputPath).ToUpperInvariant() switch | ||
{ | ||
".JSON" => openApiDocument.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0), | ||
_ => openApiDocument.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0), | ||
}; | ||
|
||
await File.WriteAllTextAsync(outputPath, text).ConfigureAwait(false); | ||
|
||
Console.WriteLine("Done."); | ||
} | ||
} |
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