-
Notifications
You must be signed in to change notification settings - Fork 879
Honor resource command confirmations in CLI #16669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,6 +24,7 @@ internal static class ResourceCommandHelper | |||||||||
| /// <param name="progressVerb">The verb to display during progress (e.g., "Starting", "Stopping").</param> | ||||||||||
| /// <param name="baseVerb">The base verb for error messages (e.g., "start", "stop").</param> | ||||||||||
| /// <param name="pastTenseVerb">The past tense verb for success messages (e.g., "started", "stopped").</param> | ||||||||||
| /// <param name="confirmationBinding">The binding that controls command confirmation prompts.</param> | ||||||||||
| /// <param name="cancellationToken">Cancellation token.</param> | ||||||||||
| /// <returns>Exit code indicating success or failure.</returns> | ||||||||||
| public static async Task<int> ExecuteResourceCommandAsync( | ||||||||||
|
|
@@ -35,10 +36,17 @@ public static async Task<int> ExecuteResourceCommandAsync( | |||||||||
| string progressVerb, | ||||||||||
| string baseVerb, | ||||||||||
| string pastTenseVerb, | ||||||||||
| PromptBinding<bool>? confirmationBinding, | ||||||||||
| CancellationToken cancellationToken) | ||||||||||
| { | ||||||||||
| logger.LogDebug("{Verb} resource '{ResourceName}'", progressVerb, resourceName); | ||||||||||
|
|
||||||||||
| if (!await ConfirmExecutionAsync(connection, interactionService, resourceName, commandName, confirmationBinding, cancellationToken).ConfigureAwait(false)) | ||||||||||
| { | ||||||||||
| interactionService.DisplayMessage(KnownEmojis.Warning, $"{progressVerb} command for '{resourceName}' was canceled."); | ||||||||||
| return ExitCodeConstants.FailedToExecuteResourceCommand; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| var response = await interactionService.ShowStatusAsync( | ||||||||||
| $"{progressVerb} resource '{resourceName}'...", | ||||||||||
| async () => await connection.ExecuteResourceCommandAsync(resourceName, commandName, cancellationToken)); | ||||||||||
|
|
@@ -55,13 +63,20 @@ public static async Task<int> ExecuteGenericCommandAsync( | |||||||||
| ILogger logger, | ||||||||||
| string resourceName, | ||||||||||
| string commandName, | ||||||||||
| PromptBinding<bool>? confirmationBinding, | ||||||||||
| CancellationToken cancellationToken) | ||||||||||
| { | ||||||||||
| logger.LogDebug("Executing command '{CommandName}' on resource '{ResourceName}'", commandName, resourceName); | ||||||||||
|
|
||||||||||
| // Route status messages to stderr so command results in stdout remain pipeable (e.g., | jq) | ||||||||||
| interactionService.Console = ConsoleOutput.Error; | ||||||||||
|
|
||||||||||
| if (!await ConfirmExecutionAsync(connection, interactionService, resourceName, commandName, confirmationBinding, cancellationToken).ConfigureAwait(false)) | ||||||||||
| { | ||||||||||
| interactionService.DisplayMessage(KnownEmojis.Warning, $"Command '{commandName}' on '{resourceName}' was canceled."); | ||||||||||
| return ExitCodeConstants.FailedToExecuteResourceCommand; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| var response = await interactionService.ShowStatusAsync( | ||||||||||
| $"Executing command '{commandName}' on resource '{resourceName}'...", | ||||||||||
| async () => await connection.ExecuteResourceCommandAsync(resourceName, commandName, cancellationToken)); | ||||||||||
|
|
@@ -91,6 +106,40 @@ public static async Task<int> ExecuteGenericCommandAsync( | |||||||||
| return response.Success ? ExitCodeConstants.Success : ExitCodeConstants.FailedToExecuteResourceCommand; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private static async Task<bool> ConfirmExecutionAsync( | ||||||||||
| IAppHostAuxiliaryBackchannel connection, | ||||||||||
| IInteractionService interactionService, | ||||||||||
| string resourceName, | ||||||||||
| string commandName, | ||||||||||
| PromptBinding<bool>? confirmationBinding, | ||||||||||
| CancellationToken cancellationToken) | ||||||||||
| { | ||||||||||
| var confirmationMessage = await GetConfirmationMessageAsync(connection, resourceName, commandName, cancellationToken).ConfigureAwait(false); | ||||||||||
| if (string.IsNullOrWhiteSpace(confirmationMessage)) | ||||||||||
| { | ||||||||||
| return true; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return await interactionService.PromptConfirmAsync(confirmationMessage, confirmationBinding, cancellationToken).ConfigureAwait(false); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private static async Task<string?> GetConfirmationMessageAsync( | ||||||||||
| IAppHostAuxiliaryBackchannel connection, | ||||||||||
| string resourceName, | ||||||||||
| string commandName, | ||||||||||
| CancellationToken cancellationToken) | ||||||||||
| { | ||||||||||
| var snapshots = await connection.GetResourceSnapshotsAsync(includeHidden: true, cancellationToken).ConfigureAwait(false); | ||||||||||
| var resources = ResourceSnapshotMapper.ResolveResources(resourceName, snapshots); | ||||||||||
| if (resources.Count == 0) | ||||||||||
| { | ||||||||||
| return null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| var command = resources[0].Commands.FirstOrDefault(c => string.Equals(c.Name, commandName, StringComparison.OrdinalIgnoreCase)); | ||||||||||
|
||||||||||
| var command = resources[0].Commands.FirstOrDefault(c => string.Equals(c.Name, commandName, StringComparison.OrdinalIgnoreCase)); | |
| var command = resources[0].Commands.FirstOrDefault(c => | |
| string.Equals(c.Name, commandName, StringComparison.OrdinalIgnoreCase) && | |
| string.Equals(c.State, "Enabled", StringComparison.OrdinalIgnoreCase)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--non-interactiveis already defined as a recursive global option onRootCommand(seeRootCommand.NonInteractiveOption). Adding a secondOption<bool>("--non-interactive")onResourceCommandcan create ambiguous binding depending on where the flag appears (e.g.aspire --non-interactive resource ...may bind to the root option while this code reads the resource-scoped option), which would break the intended auto-confirm behavior in non-interactive runs. Consider removing the resource-scoped option and instead usingRootCommand.NonInteractiveOptionforPromptBinding.Create(...)(or introduce a distinct option name for the resource-confirmation override).