Skip to content

Commit 4a06321

Browse files
committed
chore: refactors command definition
Signed-off-by: Vincent Biret <vincentbiret@hotmail.com>
1 parent cf6343e commit 4a06321

File tree

1 file changed

+5
-43
lines changed

1 file changed

+5
-43
lines changed

src/tool/OverlayCliApp.cs

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ internal static class OverlayCliApp
2222
public static async Task<int> RunAsync(string[] args, CancellationToken cancellationToken = default)
2323
{
2424
var rootCommand = new RootCommand("BinkyLabs OpenAPI Overlays CLI - Apply overlays to OpenAPI documents");
25-
var applyCommand = CreateApplyCommand();
25+
var applyCommand = CreateApplyCommand("apply", "Apply one or more overlays to an OpenAPI document", ApplyOverlaysAsync);
2626
rootCommand.Add(applyCommand);
27-
var applyAndNormalizeCommand = CreateApplyAndNormalizeCommand();
27+
var applyAndNormalizeCommand = CreateApplyCommand("apply-and-normalize", "Apply one or more overlays to an OpenAPI document, and normalize the output with OpenAPI.net", ApplyOverlaysAndNormalizeAsync);
2828
rootCommand.Add(applyAndNormalizeCommand);
2929
return await rootCommand.Parse(args).InvokeAsync(cancellationToken: cancellationToken);
3030
}
@@ -52,9 +52,9 @@ private static Option<string> CreateOutputOption()
5252
return outputOption;
5353
}
5454

55-
private static Command CreateApplyCommand()
55+
private static Command CreateApplyCommand(string name, string description, Func<string, string[], string, CancellationToken, Task> applyAsync)
5656
{
57-
var applyCommand = new Command("apply-and-normalize", "Apply one or more overlays to an OpenAPI document, and normalize the output with OpenAPI.net");
57+
var applyCommand = new Command(name, description);
5858

5959
var inputArgument = CreateInputArgument();
6060

@@ -84,51 +84,13 @@ private static Command CreateApplyCommand()
8484
return 1;
8585
}
8686

87-
await HandleCommandAsync(input, overlays ?? [], output, ApplyOverlaysAsync, cancellationToken);
87+
await HandleCommandAsync(input, overlays ?? [], output, applyAsync, cancellationToken);
8888
return 0;
8989
});
9090

9191
return applyCommand;
9292
}
9393

94-
private static Command CreateApplyAndNormalizeCommand()
95-
{
96-
var applyCommand = new Command("apply", "Apply one or more overlays to an OpenAPI document");
97-
98-
var inputArgument = CreateInputArgument();
99-
100-
var overlayOption = CreateOverlayOption();
101-
102-
var outputOption = CreateOutputOption();
103-
104-
applyCommand.Add(inputArgument);
105-
applyCommand.Add(overlayOption);
106-
applyCommand.Add(outputOption);
107-
108-
applyCommand.SetAction(async (parseResult, cancellationToken) =>
109-
{
110-
var input = parseResult.GetValue(inputArgument);
111-
var overlays = parseResult.GetValue(overlayOption);
112-
var output = parseResult.GetValue(outputOption);
113-
114-
if (string.IsNullOrEmpty(input))
115-
{
116-
await Console.Error.WriteLineAsync("Error: Input argument is required.");
117-
return 1;
118-
}
119-
120-
if (string.IsNullOrEmpty(output))
121-
{
122-
await Console.Error.WriteLineAsync("Error: Output option is required.");
123-
return 1;
124-
}
125-
126-
await HandleCommandAsync(input, overlays ?? [], output, ApplyOverlaysAndNormalizeAsync, cancellationToken);
127-
return 0;
128-
});
129-
130-
return applyCommand;
131-
}
13294
private static async Task HandleCommandAsync(
13395
string input,
13496
string[] overlays,

0 commit comments

Comments
 (0)