Skip to content

Commit 9e19914

Browse files
IEvangelistJamesNK
andauthored
Initial bits for the interaction service (#4045)
* Initial bits for the interaction service * Fix some issues * Fix build issues * Tweak rendering of code * API example * Update the article to be a bit more contextual to CLI and dashboard. * Fix APIs * More updates * Remove .aspire stuff * Enhance interaction service documentation for clarity and context in CLI and dashboard usage * Update explore.md to include interaction prompts and update metadata - closes #4108 * Apply suggestions from code review Co-authored-by: James Newton-King <[email protected]> * Address peer feedback * Update interaction service documentation and remove obsolete snippets * Clarify input validation section in interaction service documentation * Apply suggestions from code review Co-authored-by: James Newton-King <[email protected]> --------- Co-authored-by: James Newton-King <[email protected]>
1 parent 5b3c96f commit 9e19914

19 files changed

+754
-1
lines changed

docs/extensibility/interaction-service.md

Lines changed: 359 additions & 0 deletions
Large diffs are not rendered by default.
Loading
Loading
Loading
Loading
Loading
Loading
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
3+
partial class Program
4+
{
5+
public static async Task<ExecuteCommandResult> ShowConfirmationExample(ExecuteCommandContext context)
6+
{
7+
// <example>
8+
var interactionService = context.ServiceProvider.GetRequiredService<IInteractionService>();
9+
10+
// Prompt for confirmation before resetting database
11+
var resetConfirmation = await interactionService.PromptConfirmationAsync(
12+
title: "Confirm Reset",
13+
message: "Are you sure you want to reset the `development-database`? This action **cannot** be undone.",
14+
options: new MessageBoxInteractionOptions
15+
{
16+
Intent = MessageIntent.Confirmation,
17+
PrimaryButtonText = "Reset",
18+
SecondaryButtonText = "Cancel",
19+
ShowSecondaryButton = true,
20+
EnableMessageMarkdown = true
21+
});
22+
23+
if (resetConfirmation.Data is true)
24+
{
25+
// Perform the reset operation...
26+
27+
return CommandResults.Success();
28+
}
29+
else
30+
{
31+
return CommandResults.Failure("Database reset canceled by user.");
32+
}
33+
// </example>
34+
}
35+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
3+
partial class Program
4+
{
5+
public static async Task<ExecuteCommandResult> ShowMessageBoxExample(ExecuteCommandContext context)
6+
{
7+
// <example>
8+
var interactionService = context.ServiceProvider.GetRequiredService<IInteractionService>();
9+
10+
var result = await interactionService.PromptMessageBoxAsync(
11+
"Simple Message Box: Example",
12+
"""
13+
##### 🤓 Nice!
14+
15+
It's worth noting that **Markdown** is _supported_
16+
(and demonstrated here) in the message body. Simply
17+
configure the options as:
18+
19+
```csharp
20+
var options = new MessageBoxInteractionOptions
21+
{
22+
EnableMessageMarkdown = true,
23+
// Other options...
24+
};
25+
```
26+
27+
Cool, [📖 learn more](https://learn.microsoft.com/dotnet/aspire/extensibility/interaction-service)...
28+
""",
29+
new MessageBoxInteractionOptions
30+
{
31+
EnableMessageMarkdown = true,
32+
PrimaryButtonText = "Awesome"
33+
}
34+
);
35+
36+
if (result.Canceled)
37+
{
38+
return CommandResults.Failure("User cancalled.");
39+
}
40+
41+
return result.Data
42+
? CommandResults.Success()
43+
: CommandResults.Failure("The user doesn't like the example");
44+
// </example>
45+
}
46+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Logging;
3+
4+
partial class Program
5+
{
6+
public static async Task<ExecuteCommandResult> ShowMultipleInputExample(
7+
ExecuteCommandContext context, FakeResource fakeResource)
8+
{
9+
// <example>
10+
var interactionService = context.ServiceProvider.GetRequiredService<IInteractionService>();
11+
var loggerService = context.ServiceProvider.GetRequiredService<ResourceLoggerService>();
12+
var logger = loggerService.GetLogger(fakeResource);
13+
14+
var inputs = new List<InteractionInput>
15+
{
16+
new()
17+
{
18+
Label = "Application Name",
19+
InputType = InputType.Text,
20+
Required = true,
21+
Placeholder = "my-app"
22+
},
23+
new()
24+
{
25+
Label = "Environment",
26+
InputType = InputType.Choice,
27+
Required = true,
28+
Options =
29+
[
30+
new("dev", "Development"),
31+
new("staging", "Staging"),
32+
new("test", "Testing")
33+
]
34+
},
35+
new()
36+
{
37+
Label = "Instance Count",
38+
InputType = InputType.Number,
39+
Required = true,
40+
Placeholder = "1"
41+
},
42+
new()
43+
{
44+
Label = "Enable Monitoring",
45+
InputType = InputType.Boolean,
46+
Required = false
47+
}
48+
};
49+
50+
var appConfigurationInput = await interactionService.PromptInputsAsync(
51+
title: "Application Configuration",
52+
message: "Configure your application deployment settings:",
53+
inputs: inputs);
54+
55+
if (!appConfigurationInput.Canceled)
56+
{
57+
// Process the collected input values
58+
var appName = appConfigurationInput.Data[0].Value;
59+
var environment = appConfigurationInput.Data[1].Value;
60+
var instanceCount = int.Parse(appConfigurationInput.Data[2].Value ?? "1");
61+
var enableMonitoring = bool.Parse(appConfigurationInput.Data[3].Value ?? "false");
62+
63+
logger.LogInformation("""
64+
Application Name: {AppName}
65+
Environment: {Environment}
66+
Instance Count: {InstanceCount}
67+
Monitoring Enabled: {EnableMonitoring}
68+
""",
69+
appName, environment, instanceCount, enableMonitoring);
70+
71+
// Use the collected values as needed
72+
return CommandResults.Success();
73+
}
74+
else
75+
{
76+
return CommandResults.Failure("User canceled application configuration input.");
77+
}
78+
// </example>
79+
}
80+
}

0 commit comments

Comments
 (0)