-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandExecutorExtensions.cs
More file actions
127 lines (114 loc) · 6.38 KB
/
Copy pathCommandExecutorExtensions.cs
File metadata and controls
127 lines (114 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#nullable enable
namespace EfMigrationDiff.CLI;
/// <summary>
/// Extension methods for <see cref="CommandExecutor"/> providing convenient utility operations
/// for command execution, validation, and result handling.
/// </summary>
public static class CommandExecutorExtensions
{
/// <summary>
/// Checks if a command with the given name is registered.
/// </summary>
/// <param name="executor">The command executor instance. Cannot be <see langword="null"/>.</param>
/// <param name="commandName">Name of the command to check. Cannot be <see langword="null"/> or whitespace.</param>
/// <returns><see langword="true"/> if the command is registered; otherwise, <see langword="false"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="executor"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="commandName"/> is <see langword="null"/>, empty, or consists only of whitespace.</exception>
public static bool IsCommandRegistered(this CommandExecutor executor, string commandName)
{
ArgumentNullException.ThrowIfNull(executor);
ArgumentException.ThrowIfNullOrWhiteSpace(commandName);
return executor.GetRegisteredCommandNames()
.Contains(commandName.ToLowerInvariant());
}
/// <summary>
/// Executes a command and returns the result. If the command fails, throws an exception.
/// </summary>
/// <param name="executor">The command executor instance. Cannot be <see langword="null"/>.</param>
/// <param name="commandName">Name of the command to execute. Cannot be <see langword="null"/> or empty.</param>
/// <param name="args">Command arguments. Can be empty but not <see langword="null"/>.</param>
/// <param name="serviceProvider">Service provider for dependency injection. Cannot be <see langword="null"/>.</param>
/// <param name="verbose">If true, includes detailed stack traces in error output.</param>
/// <returns>The command result.</returns>
/// <exception cref="ArgumentNullException">Thrown if any parameter is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">Thrown if the command execution fails.</exception>
public static async Task<CommandResult> ExecuteOrThrowAsync(
this CommandExecutor executor,
string commandName,
string[] args,
IServiceProvider serviceProvider,
bool verbose = false)
{
ArgumentNullException.ThrowIfNull(executor);
ArgumentNullException.ThrowIfNull(commandName);
ArgumentNullException.ThrowIfNull(args);
ArgumentNullException.ThrowIfNull(serviceProvider);
var result = await executor.ExecuteAsync(commandName, args, serviceProvider, verbose: verbose);
if (!result.Success)
{
throw new InvalidOperationException(
$"Command '{commandName}' failed with exit code {result.ExitCode}: {result.Message}");
}
return result;
}
/// <summary>
/// Executes a command with the given arguments and returns the data payload.
/// </summary>
/// <typeparam name="T">Type of the data payload.</typeparam>
/// <param name="executor">The command executor instance. Cannot be <see langword="null"/>.</param>
/// <param name="commandName">Name of the command to execute. Cannot be <see langword="null"/> or empty.</param>
/// <param name="args">Command arguments. Can be empty but not <see langword="null"/>.</param>
/// <param name="serviceProvider">Service provider for dependency injection. Cannot be <see langword="null"/>.</param>
/// <param name="verbose">If true, includes detailed stack traces in error output.</param>
/// <returns>The data payload from the command result.</returns>
/// <exception cref="ArgumentNullException">Thrown if any parameter is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">Thrown if the command fails or data is not of type <typeparamref name="T"/>.</exception>
public static async Task<T> ExecuteAndGetDataAsync<T>(
this CommandExecutor executor,
string commandName,
string[] args,
IServiceProvider serviceProvider,
bool verbose = false)
{
ArgumentNullException.ThrowIfNull(executor);
ArgumentNullException.ThrowIfNull(commandName);
ArgumentNullException.ThrowIfNull(args);
ArgumentNullException.ThrowIfNull(serviceProvider);
var result = await executor.ExecuteAsync(commandName, args, serviceProvider, verbose: verbose);
if (!result.Success)
{
throw new InvalidOperationException(
$"Command '{commandName}' failed: {result.Message}");
}
if (result.Data is not T data)
{
throw new InvalidOperationException(
$"Command '{commandName}' did not return data of type {typeof(T).Name}");
}
return data;
}
/// <summary>
/// Executes a command and returns the result. If the command fails, returns the error result.
/// This method provides a safe execution path that never throws exceptions.
/// </summary>
/// <param name="executor">The command executor instance. Cannot be <see langword="null"/>.</param>
/// <param name="commandName">Name of the command to execute. Cannot be <see langword="null"/> or empty.</param>
/// <param name="args">Command arguments. Can be empty but not <see langword="null"/>.</param>
/// <param name="serviceProvider">Service provider for dependency injection. Cannot be <see langword="null"/>.</param>
/// <param name="verbose">If true, includes detailed stack traces in error output.</param>
/// <returns>The command result, successful or failed.</returns>
/// <exception cref="ArgumentNullException">Thrown if any parameter is <see langword="null"/>.</exception>
public static async Task<CommandResult> ExecuteWithFallbackAsync(
this CommandExecutor executor,
string commandName,
string[] args,
IServiceProvider serviceProvider,
bool verbose = false)
{
ArgumentNullException.ThrowIfNull(executor);
ArgumentNullException.ThrowIfNull(commandName);
ArgumentNullException.ThrowIfNull(args);
ArgumentNullException.ThrowIfNull(serviceProvider);
return await executor.ExecuteAsync(commandName, args, serviceProvider, verbose: verbose);
}
}