forked from Azure/azure-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyValueListCommand.cs
More file actions
88 lines (71 loc) · 3.22 KB
/
Copy pathKeyValueListCommand.cs
File metadata and controls
88 lines (71 loc) · 3.22 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using AzureMcp.AppConfig.Commands;
using AzureMcp.AppConfig.Models;
using AzureMcp.AppConfig.Options;
using AzureMcp.AppConfig.Options.KeyValue;
using AzureMcp.AppConfig.Services;
using AzureMcp.Core.Commands;
using AzureMcp.Core.Services.Telemetry;
using Microsoft.Extensions.Logging;
namespace AzureMcp.AppConfig.Commands.KeyValue;
public sealed class KeyValueListCommand(ILogger<KeyValueListCommand> logger) : BaseAppConfigCommand<KeyValueListOptions>()
{
private const string CommandTitle = "List App Configuration Key-Value Settings";
private readonly ILogger<KeyValueListCommand> _logger = logger;
// KeyValueList has different key and label descriptions, which is why we are defining here instead of using BaseKeyValueCommand
private readonly Option<string> _keyOption = AppConfigOptionDefinitions.KeyValueList.Key;
private readonly Option<string> _labelOption = AppConfigOptionDefinitions.KeyValueList.Label;
public override string Name => "list";
public override string Description =>
"""
List all key-values in an App Configuration store. This command retrieves and displays all key-value pairs
from the specified store. Each key-value includes its key, value, label, content type, ETag, last modified
time, and lock status.
""";
public override string Title => CommandTitle;
public override ToolMetadata Metadata => new() { Destructive = false, ReadOnly = true };
protected override void RegisterOptions(Command command)
{
base.RegisterOptions(command);
command.AddOption(_keyOption);
command.AddOption(_labelOption);
}
protected override KeyValueListOptions BindOptions(ParseResult parseResult)
{
var options = base.BindOptions(parseResult);
options.Key = parseResult.GetValueForOption(_keyOption);
options.Label = parseResult.GetValueForOption(_labelOption);
return options;
}
public override async Task<CommandResponse> ExecuteAsync(CommandContext context, ParseResult parseResult)
{
var options = BindOptions(parseResult);
try
{
if (!Validate(parseResult.CommandResult, context.Response).IsValid)
{
return context.Response;
}
context.Activity?.WithSubscriptionTag(options);
var appConfigService = context.GetService<IAppConfigService>();
var settings = await appConfigService.ListKeyValues(
options.Account!,
options.Subscription!,
options.Key,
options.Label,
options.Tenant,
options.RetryPolicy);
context.Response.Results = ResponseResult.Create(
new KeyValueListCommandResult(settings),
AppConfigJsonContext.Default.KeyValueListCommandResult);
}
catch (Exception ex)
{
_logger.LogError("An exception occurred processing command. Exception: {Exception}", ex);
HandleException(context, ex);
}
return context.Response;
}
internal record KeyValueListCommandResult(List<KeyValueSetting> Settings);
}