Skip to content

Commit 185df50

Browse files
committed
cli: Added simplify command.
1 parent 09a4a71 commit 185df50

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.CommandLine;
2+
using Microsoft.OpenApi;
3+
using Microsoft.OpenApi.Extensions;
4+
using OpenApiGenerator.Core.Extensions;
5+
6+
namespace OpenApiGenerator.Cli.Commands;
7+
8+
public class SimplifyCommand : Command
9+
{
10+
public SimplifyCommand() : base(name: "simplify", description: "Simplifies OpenAPI spec.")
11+
{
12+
var inputOption = new Argument<string>(
13+
name: "input",
14+
getDefaultValue: () => string.Empty,
15+
description: "Input file path");
16+
var outputOption = new Option<string>(
17+
aliases: ["--output", "-o"],
18+
getDefaultValue: () => "simplified.yaml",
19+
description: "Output file path");
20+
AddArgument(inputOption);
21+
AddOption(outputOption);
22+
23+
this.SetHandler(
24+
HandleAsync,
25+
inputOption,
26+
outputOption);
27+
}
28+
29+
private static async Task HandleAsync(
30+
string inputPath,
31+
string outputPath)
32+
{
33+
Console.WriteLine($"Loading {inputPath}...");
34+
35+
using var client = new HttpClient();
36+
var yamlOrJson = inputPath.StartsWith("http", StringComparison.OrdinalIgnoreCase)
37+
? await client.GetStringAsync(new Uri(inputPath)).ConfigureAwait(false)
38+
: await File.ReadAllTextAsync(inputPath).ConfigureAwait(false);
39+
var openApiDocument = yamlOrJson.GetOpenApiDocument();
40+
41+
Console.WriteLine("Simplifying...");
42+
43+
openApiDocument = openApiDocument.Simplify();
44+
45+
var text = Path.GetExtension(outputPath).ToUpperInvariant() switch
46+
{
47+
".JSON" => openApiDocument.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0),
48+
_ => openApiDocument.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0),
49+
};
50+
51+
await File.WriteAllTextAsync(outputPath, text).ConfigureAwait(false);
52+
53+
Console.WriteLine("Done.");
54+
}
55+
}

src/libs/OpenApiGenerator.Cli/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
var rootCommand = new RootCommand(
55
description: "CLI tool to use OpenApiGenerator");
66
rootCommand.AddCommand(new GenerateCommand());
7+
rootCommand.AddCommand(new SimplifyCommand());
78

89
return await rootCommand.InvokeAsync(args).ConfigureAwait(false);

src/tests/OpenApiGenerator.IntegrationTests.Cli/CliTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class CliTests
2020
[DataRow("twitch.json")]
2121
[DataRow("https://dedoose-rest-api.onrender.com/swagger/v1/swagger.json")]
2222
[DataRow("together.yaml")]
23-
public async Task Run(string spec)
23+
public async Task Generate(string spec)
2424
{
2525
var tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
2626
try

0 commit comments

Comments
 (0)